我定义了两个占位符,x与无形状,11和y与无形状,10。使用无作为第一维,我应该能够使用不同批处理大小的模型。
如果我在随机梯度下降模式下运行,使用批处理大小1,一切正常。
sess.run(train, {x: [df.values[i][0:11]], y: [df.values[i][11:]]})在这种情况下,占位符x和y的形状为(1,11)和(1,10)。
如果在完全批处理梯度下降模式下运行,使用批处理大小为1000,则会得到不兼容的矩阵操作错误。在这种情况下,占位符x和y的形状为(1000,11)和(1000,10)。
InvalidArgumentError (回溯见上文):不兼容的形状: 10,10比1000,10[节点:渐变/Sub_grad/BroadcastGradientArgs=BroadcastGradientArgsdevice="/job:localhost/replica:0/task:0/cpu:0"]
当然,我不能减去a (10,10)和a (1000,10)。但我认为TensorFlow会为我处理“批次大小”吗?谢谢。
import pandas as pd
import tensorflow as tf
import numpy
## Import the Dummy Data from Excel
df = pd.read_excel("../data/DummyData.xlsx", sheetname=0, header=0, skiprows=1 )
x = tf.placeholder(tf.float32, shape=[None,11])
y = tf.placeholder(tf.float32, shape=[None,10])
# layer 1
W1 = tf.Variable(tf.random_normal(shape=[11,10]))
b1 = tf.Variable(tf.random_normal(shape=[10,1]))
prop_fn_1 = tf.matmul(x,W1) + b1
akt_fn_1 = tf.sigmoid(prop_fn_1)
# layer2
W2 = tf.Variable(tf.random_normal(shape=[10,10]))
b2 = tf.Variable(tf.random_normal(shape=[10,1]))
prop_fn_2 = tf.matmul(prop_fn_1, W2) + b2
akt_fn_2 = tf.sigmoid(prop_fn_2)
init = tf.global_variables_initializer()
# error
loss = tf.reduce_sum(tf.square(tf.subtract(akt_fn_2,y)))
opt = tf.train.GradientDescentOptimizer(0.0001)
train = opt.minimize(loss)
# Train Stochastic
# Using Gradient Descent
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train, {x: [df.values[i][0:11]], y: [df.values[i][11:]]})
if i % 100 == 0:
print( sess.run(loss,{x: [df.values[i][0:11]], y: [df.values[i][11:]]} ))
sess.close()
print("*****************")
# Train with Max Batch Size
# Using Gradient Descent
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train, feed_dict={x: df.values[:,:11], y: df.values[:,11:]})
if i % 100 == 0:
print(sess.run(loss, feed_dict={x: df.values[:,:11], y: df.values[:,11:]}))
sess.close()发布于 2017-03-15 08:28:01
你能帮我试试这个吗?
b1 = tf.Variable(tf.random_normal(shape=[10]))
b2 = tf.Variable(tf.random_normal(shape=[10]))https://stackoverflow.com/questions/42803767
复制相似问题