我正在尝试使用神经网络解决kaggle的泰坦尼克号机器学习挑战。我删除了大多数不相关的数据,并将有用的数据转换为2D numpy数组,而将生存期转换为1D numpy数组。由于某种原因,它会抛出一个错误,指出两个形状的尺寸0必须相等。我已经尝试解决了很长时间,希望大家能帮忙。

TensorFlowNumpy.py

import tensorflow as tf

def numpy2tensor(numpy):
    sess = tf.Session()
    with sess.as_default():
        return tf.constant(numpy)

def tensor2numpy(tensor):
    sess = tf.Session()
    with sess.as_default():
        return tensor.eval()


数据集

import pandas
import numpy as np
dataset = pandas.read_csv('train.csv')
dataset2= dataset.drop(['PassengerId','Survived','Name','Ticket','Fare','Cabin','Embarked'],axis=1)
dataset3= dataset2.fillna(0)
survive = pandas.read_csv('train.csv')
survival = np.float32(survive.Survived)
dataset4 = np.float32(dataset3)


MainCode.py

import tensorflow as tf
import numpy
from dataset import dataset4,survival
from sklearn.model_selection import train_test_split
from TensorFlowNumpy import numpy2tensor
train_x,test_x,train_y,test_y = train_test_split(dataset4,survival,test_size
= 0.2)
tensor_train_x = numpy2tensor(train_x)
tensor_train_y = numpy2tensor(train_y)
tensor_test_x = numpy2tensor(test_x)
tensor_test_y = numpy2tensor(test_y)

n_nodes_hl1 = 10
n_nodes_hl2 = 10
n_classes = 2

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

def neural_network_model(data):
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([5,
n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
    hidden_2_layer = {'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_1_layer['weights']),
hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']),
hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
output = tf.matmul(l2,output_layer['weights']) + output_layer['biases']

return output

def train_neural_network(x):
    prediction = neural_network_model(x)
    cost =
tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,
labels=tensor_train_y))
     optimizer1 = tf.train.GradientDescentOptimizer(0.001).minimize(cost)

    hm_epochs = 100
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(hm_epochs):
            epoch_loss = 0
            _, c = sess.run([optimizer1, cost], feed_dict={x:tensor_train_x,
y:tensor_train_y})
            epoch_loss += c
            print('Epoch', epoch+1, '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:test_x, y:test_y}))

train_neural_network(tensor_train_x)

最佳答案

我已经多次遇到此错误,问题显然在我们的代码中。我今天离开时没有仔细检查您的代码,但我闻到您的因变量/输出变量形状为[1,712],应为[712,1],因此代码中的某些地方尝试对其进行修复。基本上,这意味着您有712行的一行,但应该是712行的1列(输出)。如果有帮助,请将其标记为答案。如果问题仍然存在,请明天给我打电话。我会看一下。

10-06 06:29