Tensorflow抛出"尺寸必须相等,但对于MatMul'是100和0。 (op:' MatMul')输入形状:[0,100],[0,100]。"

时间:2018-05-04 13:23:16

标签: python tensorflow

我正在尝试在教程之后学习tensorflow,但我不想使用mnist数据库,所以我可以在python中学习数据库管理(我是新手,它是一个很难学习的曲线从c ++ / java转到它)

所以,这是我的代码。我尝试过印刷形状,价值观和所有类型的东西,但似乎都没有。注意:如果我制作形状[0,100]的x和权重[100,0],则来自matmul的误差消失,但结果是形状[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)

1 个答案:

答案 0 :(得分:0)

而不是0,您的占位符应该为第一个维度(批量维度)None,并且以下维度应该是描述向量/矩阵的大小。

例如,x = tf.placeholder("float", [None, 64, 64, 3])将成为一批64 x 64像素RGB彩色图像的占位符。

执行2D矩阵乘法时,第一个操作数的列数必须与第二个操作数的行数匹配。这就是定义toolz的方式。