[英]Shape must be rank 2 but is rank 3 for 'MatMul_46' (op: 'MatMul') with input shapes: [100,100], [?,15,100]
[英]Tensorflow throws “Dimensions must be equal, but are 100 and 0 for 'MatMul' (op: 'MatMul') with input shapes: [0,100], [0,100].”
我试图在学习完教程后学习tensorflow,但是我不想使用mnist数据库,所以我可以在python中学习数据库管理(我是新手,从c ++ / java到它都是很难的学习曲线)
所以,这是我的代码。 我曾尝试打印形状,值和各种各样的东西,但似乎都没有用。 注意:如果我使x的形状为[0,100],权重[100,0],则来自matmul的误差会消失,但是resul的形状为[0,0],并且不能添加到偏差中。 我100%肯定这是一个新手错误,但是如果您有任何帮助,我将不胜感激。 提前致谢。
import tensorflow as tf
import pandas as pd
data = pd.read_csv('trainingData.txt', sep = "\t", header = None )
data.columns = ["in", "out"]
data_x = data.loc[: , "in"]
data_y = data.loc[: , "out"]
n_noduri_hl1 = 100
n_noduri_hl2 = 250
n_noduri_hl3 = 100
batch_size = 100
x = tf.placeholder("float", [0, 100])
y = tf.placeholder('float')
def Neural_Network(data):
# input * wheight + bias
hidden_1 = {'weight': tf.Variable(tf.random_normal([0, n_noduri_hl1])),
'biases': tf.Variable(tf.random_normal([n_noduri_hl1]))}
hidden_2 = {'weight': tf.Variable(tf.random_normal([n_noduri_hl1, n_noduri_hl2])),
'biases': tf.Variable(tf.random_normal([n_noduri_hl2]))}
hidden_3 = {'weight': tf.Variable(tf.random_normal([n_noduri_hl2, n_noduri_hl3])),
'biases': tf.Variable(tf.random_normal([n_noduri_hl3]))}
output_layer = {'weight': tf.Variable(tf.random_normal([n_noduri_hl3, 1])),
'biases': tf.Variable(tf.random_normal([1]))}
#calcul
print("data: ", data, "matmul: ", tf.matmul(data, hidden_1['weight']))
l1 = tf.add(tf.matmul(data, hidden_1['weight']), hidden_1['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_2['weight']), hidden_2['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_3['weight']), hidden_3['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weight']) + output_layer['biases']
return output
def get_next_batch(dataptr, batch_size, index):
batch = dataptr.loc[index: index+batch_size]
print(batch)
return batch
def train(x):
predictie = Neural_Network(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits = predictie, labels = y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
epoci = 10
index = 0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoca in range(epoci):
loss = 0
for _ in range(int(len(data)/batch_size)):
ep_x = get_next_batchin(data_x, batch_size, index)
ep_y = get_next_batchout(data_ybatch_size, index)
index += batch_size
_, c = sess.run([optimizer, cost], feed_dict = {x: ep_x, y: ep_y})
loss += c
print('Epoca: ', epoca, " din ", epoci, " loss: ", loss)
corect = tf.equal(tf.argmax(predictie, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(corect, 'float'))
print('Acuratete: ', accuracy.eval({x: data.loc[: , "in"], y: data.loc[: , "out"]}))
train(x)
而不是0
,您的占位符应具有None
所述第一尺寸(批次尺寸)和以下尺寸应该是描述矢量/矩阵的大小。
例如, x = tf.placeholder("float", [None, 64, 64, 3])
将是一批64 x 64像素RGB彩色图像的占位符。
执行2D矩阵乘法时,第一个操作数的列数必须与第二个操作数的行数匹配。 这就是定义矩阵乘法的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.