Ich möchte eine lineare Regression mit Tensorflow realisieren. Aber ich weiß nicht, was daran falsch ist. Wenn ich nur einmal trainiere, ist das vorhergesagte Ergebnis alles 0. Und wenn ich mehr trainiere, steigt der Verlust anstatt zu sinken. Kann mir jemand helfen? Danke vielmals!warum Tensorflow lineare Regression alle 0 vorherzusagen?
# Step2
x = tf.placeholder(tf.float64, [None, 14])
y_ = tf.placeholder(tf.float64, [None])
# Step3
feature_size = int(x.shape[1])
label_size = int(1)
w = tf.Variable(tf.zeros([feature_size, label_size], dtype='float64'), name='weight')
b = tf.Variable(tf.zeros([label_size], dtype='float64'), name='bias')
# Step4
y = tf.matmul(x, w) + b
# Step5
loss = tf.reduce_sum(tf.square(y-y_))# + tf.matmul(tf.transpose(w), w)
# Step6
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
with tf.Session() as sess:
# Step7
tf.global_variables_initializer().run()
train_loss, _, w_, b_, y_pred = sess.run([loss, optimizer, w , b , y],
{x: X_train.as_matrix(), y_: y_train.as_matrix()})
, wenn ich zeigen das Ergebnis mit folgenden Code:
print("The train_loss is :{0}".format(train_loss))
print("y_pred shape:{1}\n y_pred value{0}".format(y_pred, y_pred.shape))
print("w_:{0}".format(w_))
print("b_:{0}".format(b_))
Das Ergebnis mit be:
The train_loss is :25366.999902840118
y_pred shape:(151, 1)
y_pred value[[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
...
[ 0.]]
w_:[[ -4197.62931207]
[ -5012.08767412]
[-12005.66678623]
[ 16558.73513235]
[ -7305.34601191]
[ -5714.5346788 ]
[ -9633.25591793]
[-12477.03557256]
[ -9630.39349598]
[ -7365.70395179]
[-11168.48902116]
[ -6483.21729379]
[ 2177.84048453]
[ -3059.72968574]]
b_:[ 24045.6024]
Aber die Datennutzung LIBSVM babyfat_scale Datensatz:
1.0708 1:-0.482105 2:-0.966102 3:-0.707746 4:0.585492 5:-0.492537 6:-0.514938 7:-0.598475 8:-0.69697 9:-0.411471 10:-0.465839 11:-0.621622 12:-0.287129 13:-0.0791367 14:-0.535714
1.0853 1:-0.743158 2:-1 3:-0.552422 4:0.772021 5:-0.263682 6:-0.497364 7:-0.654384 8:-0.562998 9:-0.426434 10:-0.465839 11:-0.418919 12:-0.435644 13:0.136691 14:-0.142857
Und wenn ich versuche, 100 mal mit zu trainieren die gleichen Daten:
for i in range(100):
train_loss, _, w_, b_, y_pred = sess.run([loss, optimizer, w , b , y],
{x: X_train.as_matrix(), y_: y_train.as_matrix()})
Der Verlust erhöhen statt abnehmen !!! WARUM? Bitte helfen Sie mir! Danke vielmals!
I train_test_split verwenden abnehm (x, y, test_size = 0,4, random_state = 0) die Zugdaten und Testdaten zu teilen. Ich versuche, die Schrittgröße von 0,5 auf 0,01 zu ändern, wie Sie gesagt haben. Aber der Verlust steigt noch. Ich bin sehr verwirrt. –