感谢您查看此问题。
我正在尝试培训3层NN,以根据前15天的股价预测未来10天的股价。使用GradientDescentOptimizer时,变量的权重没有改变,因此希望寻求一些帮助。我尝试过以下方法:
我正在运行的代码如下。为清楚起见,此处未定义某些符号。感谢你对此事的善意建议!
#Setting value placeholder
x = tf.placeholder(tf.float64,shape=(19,15,1), name = 'Input')
y_ = tf.placeholder(tf.float64,shape=(19,10,1), name = 'Output')
#Setting DNN key architectural values
n_layers = 3
n_nodes_l1 = 20
n_nodes_l2 = 30
n_nodes_l3 = 10
W01 = tf.Variable(tf.random_uniform([n_nodes_l1, 15],0,1,dtype=tf.float64,name="W01"))
W02 = tf.Variable(tf.random_uniform([n_nodes_l2, n_nodes_l1],0,1,dtype=tf.float64),name='W02')
W03 = tf.Variable(tf.random_uniform([n_nodes_l3, n_nodes_l2],0,1,dtype=tf.float64),name='W03')
b01 = tf.Variable(tf.random_uniform([n_nodes_l1,1],0,1,dtype=tf.float64),name='b01')
b02 = tf.Variable(tf.random_uniform([n_nodes_l2,1],0,1,dtype=tf.float64),name='b02')
b03 = tf.Variable(tf.random_uniform([n_nodes_l3,1],0,1,dtype=tf.float64),name='b03')
#Building the architecture
def neural(X):
a01 = tf.matmul(W01, X) + b01
X2 = tf.sigmoid(a01)
a02 = tf.matmul(W02, X2) + b02
X3 = tf.sigmoid(a02)
a03 = tf.matmul(W03, X3) + b03
y_prediction= tf.sigmoid(a03)
return y_prediction
#Loss and Optimizer
loss = []
final_loss= []
y_pred_col = []
for n_batch in range(0,len(x_data)):
y_pred = neural(x[n_batch])
y_pred_col.append(y_pred)
loss = tf.reduce_mean(tf.square(y_ - y_pred_col))
optimizer = tf.train.GradientDescentOptimizer(0.0005).minimize(loss)
#Setting up Tensor Session
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
n_steps = 30
for iter in range(n_steps):
_, l, W01_train = sess.run([optimizer,loss,W01], feed_dict = {x: x_data, y_: y_data})
print(l)
答案 0 :(得分:0)
我会做一些不同的事情。在您的代码中有一些没有意义的东西:
for n_batch in range(0,len(x_data)):
y_pred = neural(x[n_batch])
y_pred_col.append(y_pred)
此处,neural
的每次调用都会创建一个新的神经网络,因此您最终拥有len(x_data)
个网络。我想你想要一个网络。在这种情况下,您应该只调用neural
一次:
y_pred = neural(x)
这将要求您以不同的方式定义来自neural
的{{3}}操作(因为现在您需要考虑X
的第一个维度)。然后将损失函数定义为:
loss = tf.reduce_mean(tf.square(y_ - y_pred))
全部放在一起:
#Setting value placeholder
x = tf.placeholder(tf.float64,shape=(None,15), name = 'Input')
y_ = tf.placeholder(tf.float64,shape=(None,10), name = 'Output')
#Setting DNN key architectural values
n_layers = 3
n_nodes_l1 = 20
n_nodes_l2 = 30
n_nodes_l3 = 10
W01 = tf.Variable(tf.random_uniform([15, n_nodes_l1],0,1,dtype=tf.float64,name="W01"))
W02 = tf.Variable(tf.random_uniform([n_nodes_l1, n_nodes_l2],0,1,dtype=tf.float64),name='W02')
W03 = tf.Variable(tf.random_uniform([n_nodes_l2, n_nodes_l3],0,1,dtype=tf.float64),name='W03')
b01 = tf.Variable(tf.random_uniform([n_nodes_l1],0,1,dtype=tf.float64),name='b01')
b02 = tf.Variable(tf.random_uniform([n_nodes_l2],0,1,dtype=tf.float64),name='b02')
b03 = tf.Variable(tf.random_uniform([n_nodes_l3],0,1,dtype=tf.float64),name='b03')
#Building the architecture
def neural(X):
a01 = tf.matmul(X, W01) + b01
X2 = tf.sigmoid(a01)
a02 = tf.matmul(X2, W02) + b02
X3 = tf.sigmoid(a02)
a03 = tf.matmul(X3, W03) + b03
y_prediction= tf.sigmoid(a03)
return y_prediction
#Loss and Optimizer
y_pred = neural(x)
loss = tf.reduce_mean(tf.square(y_ - y_pred))
optimizer = tf.train.GradientDescentOptimizer(0.0005).minimize(loss)
#Setting up Tensor Session
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
n_steps = 30
for iter in range(n_steps):
_, l, W01_train = sess.run([optimizer,loss,W01], feed_dict = {x: x_data, y_: y_data})
print(l)
请注意,为方便起见,我更改了占位符和权重的定义。如果x_data
和y_data
的形状分别为(batch_size=19,15)
和(batch_size=19,10)
,则上述代码将会运行。如果在此修改后问题仍然存在,则可能是由于其他原因(即取决于您的数据或超参数)。