2017-05-01 12 views
0

softmaxを使用してコスト関数を計算するとエラーが発生します。それは私が再構築またはlogitsがあると一致しないことができ形状を転置ない場合でも、私のlogitsとラベルの形状が不正な引数エラー:ロジットとラベルは同じサイズでなければなりません

InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[1000,2] labels_size=[1,1000] 
[[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape, Reshape_1)]] 

と一致していないことを述べている[1000,2]サイズとラベルが[1000,1]です。どのようにこの問題に取り組んでいますか?

n_nodes_hl1 = 250 
n_nodes_hl2 = 250 
n_classes = 2 
batch_size = 1000 

with open("xdf.pickle", 'rb') as f: 
    features = pickle.load(f) 
with open("ydf.pickle", 'rb') as f: 
    labels = pickle.load(f) 


def neural_network_model(data, feature_count): 
    hidden_layer_1 = {'weights': tf.Variable(tf.random_normal([feature_count, n_nodes_hl1])), 
        'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))} 
    hidden_layer_2 = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), 
       'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))} 
    output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])), 
       'biases': tf.Variable(tf.random_normal([n_classes])), } 

    l1 = tf.add(tf.matmul(data, hidden_layer_1['weights']), hidden_layer_1['biases']) 
    l1 = tf.nn.relu(l1) 
    l2 = tf.add(tf.matmul(l1, hidden_layer_2['weights']), hidden_layer_2['biases']) 
    l2 = tf.nn.relu(l2) 

    output = tf.matmul(l2, output_layer['weights']) + output_layer['biases'] 
    return output 


def train_neural_network(x, y, features, labels): 
    X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2) 

    prediction = neural_network_model(x, len(features.columns)) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)) 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 
    hm_epochs = 1 

    with tf.Session() as sess: 
    sess.run(tf.initialize_all_variables()) 
    for epoch in range(hm_epochs): 
     epoch_loss = 0 

     for i in range(int(len(X_train)/batch_size)): 
      epoch_x = X_train[i*batch_size: min((i + 1)*batch_size, len(X_train))] 
      epoch_y = y_train[i*batch_size: min((i + 1)*batch_size, len(y_train))] 
      i, c = sess.run([optimizer, cost], feed_dict = {x:epoch_x, y:epoch_y}) 
      epoch_loss += c 

     print('Epoch', epoch, ' completed out of ', hm_epochs, ' loss: ', epoch_loss) 

    correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) 
    accuracy = tf.reduce_mean(tf.cast(correct, 'float')) 

    print('Accuracy: ', accuracy.eval({x: X_test, y: y_test})) 


x = tf.placeholder('float', [None, len(features.columns)]) 
y = tf.placeholder('float') 
train_neural_network(x, y, features, labels) 

答えて

1

私はあなたが持っているデータがわからないので、私は推測できます。あなたのネットワークにはn_classesの出力ニューロンがありますが、あなたのラベルはバイナリ(0または1)であると仮定します。出力ニューロンの数を1に減らすか(2つのクラスしかないので動作します)、ラベルを1つのホットラベル(ラベル0と[0,1]の[1,0]ラベル1)。

また、tf.nn.sparse_softmax_cross_entropy_with_logits()を試してみてください。ネットワークの残りの部分を変更する必要がないように、おそらく動作します。

関連する問題