我正在尝试使用简单的张量流模型对Kaggle上的泰坦尼克号问题进行分类,数据集在这里:kaggle.com/c/titanic/data。
我尝试使用初学者MNIST教程方法来训练classifer,但是,当我运行以下代码时,所有变量都不会更新(W和b保持相同的值)
W=tf.Variable(tf.random_uniform([X_train.shape[1],1]))
b=tf.Variable(tf.zeros([1]))
tf_X_train = tf.placeholder(tf.float32, [None, X_train.shape[1]], name="X_train")
tf_y_train = tf.placeholder(tf.float32, [None,1], name="y_train")
y_output = tf.matmul(tf_X_train,W)+b
y_pred = tf.nn.softmax((tf.matmul(tf_X_train,W)+b))
loss = tf.reduce_mean(-tf.reduce_sum(tf_y_train*tf.log(y_pred),reduction_indices=[1]))
correct_prediction = tf.equal(tf.argmax(y_pred,1), tf.argmax(tf_y_train,1))
opt = tf.train.GradientDescentOptimizer(5)
opt_operation = opt.minimize(loss)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
tf.global_variables_initializer().run()
for _ in range(1000):
sess.run(opt_operation, feed_dict={tf_X_train: X_train, tf_y_train: y_train})
print sess.run([W,b],feed_dict={tf_X_train: X_train, tf_y_train: y_train})
print sess.run(y_pred,feed_dict={tf_X_train: X_train, tf_y_train: y_train})
无论我如何调整模型参数,所有W和b在训练步骤中保持不变,是否有人知道为什么
我阅读和预处理数据的方式如下:
df = pd.read_csv('train.csv')
df = df.drop(['Name', 'Ticket', 'Cabin', 'Embarked'], axis=1)
df = df.set_index(['PassengerId'])
df = df.replace({'male': 1, 'female':2})
df=df.dropna(axis=0)
X=df[df.columns.drop('Survived')]
Y=df['Survived']
X_train, X_test, y_train, y_test = train_test_split(X,Y)
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
y_train=y_train.reshape(y_train.shape[0],1)
print y_train.shape
print X_train.shape
n_samples = X_train.shape[0]