ValueError:无法为形状为((?,2)'的Tensor'labels:0'输入形状(1,1,2)的值

时间:2018-08-07 10:20:10

标签: python tensorflow

我是python和机器学习的新手。当我运行此代码时,它将出现此值错误。不幸的是,我看不到能够调整输入形状以匹配两个形状。我试图将标签的形状更改为(None,None,2),但是当我运行它时,火车成本上升为'nan'。我究竟做错了什么?我该如何解决?任何帮助将不胜感激。

import csv  
import tensorflow as tf  
import numpy as np    
import pandas as pd   
import urllib.request as request  
import matplotlib.pyplot as plt

train_data = pd.read_csv("C:/Python35/train_data.csv", sep=',', header = None)  
test_data = pd.read_csv("C:/Python35/test_data.csv", sep=',', header = None)  

X_train = np.asarray(train_data)  
X_test = np.asarray(test_data)  

train_label = pd.read_csv("C:/Python35/train_label.csv", sep=',', header = None)  
test_label = pd.read_csv("C:/Python35/test_label.csv", sep=',', header = None)  

y_train = np.asarray(train_label)  
y_test = np.asarray(test_label)  

labels_train = (np.arange(2) == y_train[:,None]).astype(np.float32)  
labels_test = (np.arange(2) == y_test[:,None]).astype(np.float32)  

inputs = tf.placeholder(tf.float32, shape=(None, X_train.shape[1]), name='inputs')
label = tf.placeholder(tf.float32, shape=(None, 2), name='labels')

hid1_size = 128   
w1 = tf.Variable(tf.random_normal([hid1_size, X_train.shape[1]], stddev=0.01), name='w1')  
b1 = tf.Variable(tf.constant(0.1, shape=(hid1_size, 1)), name='b1')  
y1 = tf.nn.dropout(tf.nn.relu(tf.add(tf.matmul(w1, tf.transpose(inputs)), b1)),  keep_prob=0.5)

hid2_size = 256  
w2 = tf.Variable(tf.random_normal([hid2_size, hid1_size], stddev=0.01), name='w2')  
b2 = tf.Variable(tf.constant(0.1, shape=(hid2_size, 1)), name='b2')  
y2 = tf.nn.dropout(tf.nn.relu(tf.add(tf.matmul(w2, y1), b2)), keep_prob=0.5)  

wo = tf.Variable(tf.random_normal([2, hid2_size], stddev=0.01), name='wo')  
bo = tf.Variable(tf.random_normal([2, 1]), name='bo')  
yo = tf.transpose(tf.add(tf.matmul(wo, y2), bo))  

lr = tf.placeholder(tf.float32, shape=(), name='learning_rate')  
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=yo, labels=label))  
optimizer = tf.train.GradientDescentOptimizer(lr).minimize(loss)  

pred = tf.nn.softmax(yo)  
pred_label = tf.argmax(pred, 1)  
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(label, 1))  
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))  

init = tf.global_variables_initializer()  

config = tf.ConfigProto()  
config.gpu_options.allow_growth = True  

sess = tf.InteractiveSession(config=config)  
sess.run(init)  

for learning_rate in [0.05, 0.01]:  
    for epoch in range(50):  
        avg_cost = 0.0  
        for i in range(X_train.shape[0]):  
            _, c = sess.run([optimizer, loss], feed_dict={lr:learning_rate,  
                                                          inputs: X_train[i, None],  
                                                          label: labels_train[i, None]})  
            avg_cost += c  
        avg_cost /= X_train.shape[0]  
        if epoch % 10 == 0:  
            print("Epoch: {:3d}    Train Cost: {:.4f}".format(epoch, avg_cost))  

acc_train = accuracy.eval(feed_dict={inputs: X_train, label: labels_train})
print("Train accuracy: {:3.2f}%".format(acc_train*100.0))

acc_test = accuracy.eval(feed_dict={inputs: X_test, label: labels_test})
print("Test accuracy:  {:3.2f}%".format(acc_test*100.0))

sess.close()

1 个答案:

答案 0 :(得分:0)

错误说明的是,尽管您的labels占位符需要一个形状为(None, 2)的数组,但您正在传递的形状为(1, 1, 2)的那个

这是在运行会话(sess.run())的行中完成的,更具体地说,是在feed_dict传递labels_train[i, None]到占位符labels的{​​{1}}中完成的。

从您的代码中,我无法分辨出您到底想使用标签做什么,但是简单的重塑就可以解决问题:

_, c = sess.run([optimizer, loss], feed_dict={lr:learning_rate,  
                                              inputs: X_train[i, None],  
                                              label: labels_train[i, None].reshape(-1, 2)}) 

这远不是最优解决问题的方法;将其视为一种解决方法,直到您更好地了解python和tensorflow。