【问题标题】:logits and labels must be broadcastable日志和标签必须是可广播的
【发布时间】:2019-12-12 19:19:41
【问题描述】:

我已经开始使用 tensorflow 并尝试通过从 analyticsvidhya.com 识别数字练习问题来实现简单的神经网络,并遵循了这篇文章: https://www.analyticsvidhya.com/blog/2016/10/an-introduction-to-implementing-neural-networks-using-tensorflow/

这是我的完整代码: https://github.com/NilSagor/AV_ml_practice/blob/master/digit_reco/digit_practise_01.ipynb

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(
    logits =output_layer, labels =y))

错误:logits 和标签必须是可广播的

图像重塑

temp = []
for img_name in train.filename:
    image_path = os.path.join(data_dir, 'train', 'Images', 'train', img_name)
    img = Image.open(filepath)
    img= np.array(img).astype('float32')
    temp.append(img)

train_x = np.stack(temp)

temp = []
for img_name in test.filename:
    image_path = os.path.join(data_dir, 'train', 'Images', 'test', img_name)   
    img = Image.open(filepath)    
    img= np.array(img).astype('float32')
    temp.append(img)

test_x = np.stack(temp)


with tf.Session() as sess:
    sess.run(init)

    for epoch in range(epochs):
        avg_cost = 0
        total_batch = int(train_data.shape[0]//batch_size)
        for i in range(total_batch):
            batch_x, batch_y = batch_creator(batch_size, train_x.shape[0], 'train')

            _,c = sess.run([optimizer, cost], feed_dict = {x: batch_x, y: batch_y})
            avg_cost += c/total_batch
        print("Epoch: ", (epoch+1), "cost: ", "{:.5f}".format(avg_cost))
    print("Training complete")

和batch_creator函数

def batch_creator(batch_size, dataset_length, dataset_name):
    """ Create batch with random samples and return appropiate format"""
    batch_mask = rng.choice(dataset_length, batch_size)
    batch_x = eval(dataset_name + "_x")[[batch_mask]].reshape(-1, input_num_units)
    batch_x = preproc(batch_x)
    if dataset_name == "train":
        batch_y = eval(dataset_name).ix[batch_mask, 'label'].values
        batch_y = dense_to_one_hot(batch_y)

    return batch_x, batch_y

weights = {
    'hidden': tf.Variable(tf.random_normal([input_num_units, hidden_num_units], seed = seed)),
    'output': tf.Variable(tf.random_normal([hidden_num_units, output_num_units], seed = seed))
}

biases = {
    'hidden': tf.Variable(tf.random_normal([hidden_num_units], seed = seed)),
    'output': tf.Variable(tf.random_normal([output_num_units], seed = seed))

}

hidden_layer = tf.add(tf.matmul(x, weights['hidden']), biases['hidden'])
hidden_layer = tf.nn.relu(hidden_layer)
output_layer = tf.matmul(hidden_layer, weights['output']) + biases['output']



cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits =output_layer, labels =y))
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)

如何消除错误以及如何高效地创建批处理?

提前致谢

【问题讨论】:

  • if dataset_name == "train_data": 不成立时batch_y 未定义,因此出现错误。
  • @jdehesa 我已经更新了如果 dataset_name == "train" 但现在显示错误 InvalidArgumentError: logits and labels must be broadcastable: logits_size=[512,10] labels_size=[128, 10] [[{{node softmax_cross_entropy_with_logits_6}}]]
  • tf.nn.sigmoid_cross_entropy_with_logits 还给出 InvalidArgumentError: Incompatible shapes: [512,10] vs. [128,10] 错误
  • 你能添加你的数据操作部分吗?

标签: python tensorflow neural-network deep-learning


【解决方案1】:

替换

    from matplotlib.pyplot import imread
    img = imread(image_path)

    from scipy.misc import imread
    img = imread(image_path, flatten=True)

这是在上面的帖子中所做的,它很重要,因为除了读取图像之外,它还将颜色层扁平化为单个灰度层。在你的情况下,(28, 28, 4)(28, 28)

【讨论】:

  • from scipy.misc import imread 已弃用,使用预处理进行更新
猜你喜欢
  • 2021-12-29
  • 2022-01-23
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-06
  • 1970-01-01
相关资源
最近更新 更多