Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow softmax\u交叉\u熵\u与逻辑错误_Python_Python 2.7_Tensorflow - Fatal编程技术网

Python tensorflow softmax\u交叉\u熵\u与逻辑错误

Python tensorflow softmax\u交叉\u熵\u与逻辑错误,python,python-2.7,tensorflow,Python,Python 2.7,Tensorflow,我定义了一个单输入层和输出层的神经网络。我的数据是csv格式,我已将其转换为tfrecord格式。使用tf.data api,我按如下方式对其进行批处理和馈送: 特征:32(批量大小)x 24(特征列) 标签:32(批量大小)x 4(onehot编码) 运行图形时,它抛出ValueError。以下是回溯: 文件“dummy.py”,第60行,在 训练摘要,sess.run([trainStep],feed\u dict={ground\u truth:Label,features:featu

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

  • 特征:32(批量大小)x 24(特征列)
  • 标签:32(批量大小)x 4(onehot编码)
运行图形时,它抛出ValueError。以下是回溯:

文件“dummy.py”,第60行,在 训练摘要,sess.run([trainStep],feed\u dict={ground\u truth:Label,features:features})

文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/client/session.py”,第895行,正在运行 运行_元数据_ptr)

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

ValueError:无法将形状(32,4)的值输入到具有形状“(?,)”的\u logits/reformate\u 2:0”的张量u'softmax\u cross\u entropy\u

以下是可以重现错误的最小代码:

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})
这4个返回值与您的返回语句不匹配:

return train_op, ground_truth_input, bottleneck_input, loss_mean
return train_op, ground_truth_input, bottleneck_input, loss_mean