【问题标题】:tensorflow softmax_cross_entropy_with_logits_v2 throws ValueErrortensorflow softmax_cross_entropy_with_logits_v2 抛出 ValueError
【发布时间】:2019-01-05 12:55:20
【问题描述】:

我已经定义了一个单一输入层和一个输出层的神经网络。我的数据是 csv 格式,我已转换为 tfrecord 格式。使用 tf.data api 我将其批处理并按如下方式提供:

  • 特征:32(批量大小)x 24(特征列)
  • 标签:32(批量大小)x 4(onehot 编码)

在运行图表时会抛出 ValueError。这是回溯:

文件“dummy.py”,第 60 行,在 train_summary, _ = sess.run([trainStep],feed_dict = {ground_truth : Label, features :Features})

文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py”,第 895 行,运行中 run_metadata_ptr)

文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py”,第 1104 行,在 _run % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))

ValueError: 无法为形状为 '(?,)' 的张量 u'softmax_cross_entropy_with_logits/Reshape_2:0' 提供形状 (32, 4) 的值

这是可以重现错误的最少代码:

import tensorflow as tf
import numpy as np

num_columns=24
num_classes=4
train_steps = 2

def model():

   ground_truth_input = tf.placeholder(tf.float32,[None,num_classes]) #onehotencoded with depth 4
   bottleneck_input = tf.placeholder(tf.float32,[None,num_columns])  #num_columns=24 keypoint features

   #fully connected 1 : 24(num_input_features)x100
   initial_value = tf.truncated_normal([num_columns, 100], stddev=0.001)
   layer1_weights = tf.Variable(initial_value, name='hidden1_weights')
   layer1_biases = tf.Variable(tf.zeros([100]), name='hidden1_biases')
   logits_hidden1 = tf.matmul(bottleneck_input, layer1_weights) + layer1_biases
   inp_activated=tf.nn.relu(logits_hidden1,name='hidden1_activation')

   #fully connected 2 : 100x4(num_classes)
   initial_value = tf.truncated_normal([100, num_classes], stddev=0.001)
   layer_weights = tf.Variable(initial_value, name='final_weights')
   layer_biases = tf.Variable(tf.zeros([num_classes]), name='final_biases')
   logits = tf.matmul(inp_activated, layer_weights) + layer_biases

   # loss function 
   loss_mean = tf.nn.softmax_cross_entropy_with_logits_v2(labels=ground_truth_input, logits=logits)

   with tf.name_scope('train'):
      optimizer = tf.train.MomentumOptimizer(learning_rate=0.1,use_nesterov=True,momentum=0.9)
      train_op = optimizer.minimize(loss_mean, global_step=tf.train.get_global_step())

   with tf.name_scope('SoftMax_Layer'):
      final_tensor = tf.nn.softmax(logits,name='Softmax')

   return train_op, ground_truth_input, bottleneck_input, loss_mean

trainStep, cross_entropy, features, ground_truth = model()

with tf.Session() as sess:
  for i in range(2):
       Label = np.eye(4)[np.random.choice(4,32)]
       Features = np.random.rand(32,24)
       train_summary, _ = sess.run([trainStep],feed_dict = {ground_truth : Label, features :Features})

【问题讨论】:

    标签: python python-2.7 tensorflow


    【解决方案1】:
    trainStep, cross_entropy, features, ground_truth = model()
    

    这 4 个返回值与您的返回语句不匹配:

    return train_op, ground_truth_input, bottleneck_input, loss_mean
    

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2018-01-03
      • 2016-01-24
      相关资源
      最近更新 更多