# 使用TensorFlow动手实现的简单的股价预测模型

### 导入并预处理数据

• 数据集：http://files.statworx.com/sp500.zip
```# Import data
data= pd.read_csv('data_stocks.csv')
# Drop date variable
data= data.drop(['DATE'],1)
# Dimensions of dataset
n= data.shape[0]
p= data.shape[1]```

### 准备训练和测试数据

```# Training and test data
train_start= 0
train_end= int(np.floor(0.8*n))
test_start= train_end+ 1
test_end= n
data_train= data[np.arange(train_start, train_end), :]
data_test= data[np.arange(test_start, test_end), :]```

### 数据缩放

```# Scale data
from sklearn.preprocessingimport MinMaxScaler
scaler= MinMaxScaler()
scaler.fit(data_train)
scaler.transform(data_train)
scaler.transform(data_test)
# Build X and y
X_train= data_train[:,1:]
y_train= data_train[:,0]
X_test= data_test[:,1:]
y_test= data_test[:,0]pycharm```

### TensorFlow简介

TensorFlow是一个深度学习和神经网络中处于领先地位的计算框架。它底层基于C++，通常通过Python进行控制（也有用于R语言的）。TensorFlow以底层计算任务的图形表示进行操作。这种方法允许用户将数学运算指定数据，变量和运算符作为图中的元素。由于神经网络实际上是数据图和数学运算，因此TensorFlow非常适合神经网络和深度学习。看看这个简单的例子：

```# Import TensorFlow
import tensorflow as tf

# Define a and b as placeholders
a= tf.placeholder(dtype=tf.int8)
b= tf.placeholder(dtype=tf.int8)

# Define the addition
c= tf.add(a, b)

# Initialize the graph
graph= tf.Session()

# Run the graph
graph.run(c, feed_dict{a:5, b:4})```

### 占位符

```# Placeholder
X= tf.placeholder(dtype=tf.float32, shape=[None, n_stocks])
Y= tf.placeholder(dtype=tf.float32, shape=[None])```

`None`表示，在这一点上我们还不知道在每个批处理中流过神经网络图的观测值的数量，使用它是为了保持灵活性。我们稍后将定义`batch_size`控制每次训练的批处理观察次数。

### 向量

```# Model architecture parameters
n_stocks= 500
n_neurons_1= 1024
n_neurons_2= 512
n_neurons_3= 256
n_neurons_4= 128
n_target= 1
# Layer 1: Variables for hidden weights and biases
W_hidden_1= tf.Variable(weight_initializer([n_stocks, n_neurons_1]))
bias_hidden_1= tf.Variable(bias_initializer([n_neurons_1]))
# Layer 2: Variables for hidden weights and biases
W_hidden_2= tf.Variable(weight_initializer([n_neurons_1, n_neurons_2]))
bias_hidden_2= tf.Variable(bias_initializer([n_neurons_2]))
# Layer 3: Variables for hidden weights and biases
W_hidden_3= tf.Variable(weight_initializer([n_neurons_2, n_neurons_3]))
bias_hidden_3= tf.Variable(bias_initializer([n_neurons_3]))
# Layer 4: Variables for hidden weights and biases
W_hidden_4= tf.Variable(weight_initializer([n_neurons_3, n_neurons_4]))
bias_hidden_4= tf.Variable(bias_initializer([n_neurons_4]))

# Output layer: Variables for output weights and biases
W_out= tf.Variable(weight_initializer([n_neurons_4, n_target]))
bias_out= tf.Variable(bias_initializer([n_target]))```

### 设计网络体系结构

```# Hidden layer
hidden_1= tf.nn.relu(tf.add(tf.matmul(X, W_hidden_1), bias_hidden_1))
hidden_2= tf.nn.relu(tf.add(tf.matmul(hidden_1, W_hidden_2), bias_hidden_2))
hidden_3= tf.nn.relu(tf.add(tf.matmul(hidden_2, W_hidden_3), bias_hidden_3))
hidden_4= tf.nn.relu(tf.add(tf.matmul(hidden_3, W_hidden_4), bias_hidden_4))

# Output layer (must be transposed)
out= tf.transpose(tf.add(tf.matmul(hidden_4, W_out), bias_out))```

### 损失函数

```# Cost function
mse= tf.reduce_mean(tf.squared_difference(out, Y))```

### 优化器

```# Optimizer
opt= tf.train.AdamOptimizer().minimize(mse)```

### 初始化器

```# Initializers
sigma= 1
weight_initializer= tf.variance_scaling_initializer(mode="fan_avg", distribution="uniform", scale=sigma)
bias_initializer= tf.zeros_initializer()```

### 拟合神经网络

epoch达到最大或者用户定义的其他停止标准，网络的训练就会停止。

```# Make Session
net= tf.Session()
# Run initializer
net.run(tf.global_variables_initializer())

# Setup interactive plot
plt.ion()
fig= plt.figure()
ax1= fig.add_subplot(111)
line1,= ax1.plot(y_test)
line2,= ax1.plot(y_test*0.5)
plt.show()

# Number of epochs and batch size
epochs= 10
batch_size= 256

for ein range(epochs):

# Shuffle training data
shuffle_indices= np.random.permutation(np.arange(len(y_train)))
X_train= X_train[shuffle_indices]
y_train= y_train[shuffle_indices]

# Minibatch training
for iin range(0,len(y_train)// batch_size):
start= i* batch_size
batch_x= X_train[start:start+ batch_size]
batch_y= y_train[start:start+ batch_size]
# Run optimizer with batch
net.run(opt, feed_dict={X: batch_x, Y: batch_y})

# Show progress
if np.mod(i,5)== 0:
# Prediction
pred= net.run(out, feed_dict={X: X_test})
line2.set_ydata(pred)
plt.title('Epoch ' + str(e)+ ', Batch ' + str(i))
file_name= 'img/epoch_' + str(e)+ '_batch_' + str(i)+ '.jpg'
plt.savefig(file_name)
plt.pause(0.01)```

1477 篇文章79 人订阅

0 条评论

## 相关文章

1153

4404

### 深度学习效果不好？试试 Batch Normalization 吧！

Batch Normalization（简称BN）自从提出之后，因为效果特别好，很快被作为深度学习的标准工具应用在了各种场合。BN大法虽然好，但是也存在一些局...

9413

1053

2317

3338

### 分类-回归树模型（CART）在R语言中的实现

CART模型 ，即Classification And Regression Trees。它和一般回归分析类似，是用来对变量进行解释和预测的工具，也是数据挖掘中...

5484

### C4.5决策树算法概念学习

•分类(Classification)就是按照某种标准给对象贴标签，再根据标签来区分归类，类别数不变。

1152

3454

### TensorFlow深度学习笔记 文本与序列的深度模型

Deep Models for Text and Sequence Rare Event 与其他机器学习不同，在文本分析里，陌生的东西（rare event）往...

20910