【问题标题】:Tensorflow netwrok parameters nog updatingTensorFlow 网络参数未更新
【发布时间】:2018-02-03 14:27:16
【问题描述】:

鸢尾花数据集分类,网络参数未更新

嘿,我尝试使用逻辑回归网络构建分类器,但我的参数没有更新,我的权重、偏差、输出和成本保持不变,有人可以帮助我吗?我不知道为什么我的参数没有更新我该如何解决这个问题?谢谢!

import tensorflow as tf 
import pandas as pd 
import matplotlib.pyplot as plt 
import numpy as np 
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split

df = pd.read_csv('/Users/Laurens-Wissels/Desktop/iris.csv')

x = np.array(df[["sepal_length","sepal_width","petal_length","petal_width"]])
scaler_model = MinMaxScaler()
x = scaler_model.fit_transform(x)
y = df["species"]


def yvalue(y):
    if y =="setosa":
        return [1,0,0]
    elif y == "versicolor":
        return [0,1,0]
    else:
        return [0,0,1]

y = y.apply(yvalue)
y = y.reshape(150,1)
x_train, x_test , y_train,y_test = train_test_split(x,y,test_size=0.3)
print(y_train)

n_features = 4
n_species = 1
traing_epochs = 2000
learning_rate = 0.0001
n_samples = 105
display_step = 50


X = tf.placeholder(tf.float32,[105,n_features])
Y = tf.placeholder(tf.float32,[105,1])
W = tf.Variable(tf.random_normal([n_features,n_species]))
b = tf.Variable(tf.random_normal([1]))

_y = tf.add(tf.matmul(X,W),b)
output = tf.nn.softmax(_y)

cost = tf.reduce_mean(tf.pow(Y - output , 2))/(2*n_samples)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(traing_epochs):

    sess.run(optimizer, feed_dict={X: x_train, Y: y_train}) # Take a gradient descent step using our inputs and labels
    sess.run(output,feed_dict={X: x_train, Y: y_train})

    # That's all! The rest of the cell just outputs debug messages. 
    # Display logs per epoch step
    if (i) % display_step == 0:
        cc = sess.run(cost, feed_dict={X: x_train, Y:y_train})

        print("_y:",_y)
        print("output:",output)
        print("w:",sess.run(W, feed_dict={X: x_train, Y:y_train}))
        print "Training step:", '%04d' % (i), "cost=",sess.run(cost, feed_dict={X: x_train, Y:y_train})  #, \"W=", sess.run(W), "b=", sess.run(b)
        print("-------------------------------------")

        plotData.append(sess.run(cost, feed_dict={X: x_train, Y:y_train})  )

print "Optimization Finished!"
training_cost = sess.run(cost, feed_dict={X: x_train, Y:y_train})
print "Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n'
plt.plot(plotData)
plt.show()

【问题讨论】:

    标签: python tensorflow dataset artificial-intelligence


    【解决方案1】:

    tf.nn.softmax 除以指数元素的总和(参见文档中的表达式)。如果维度中只有一个元素被求和(默认为最后一个):

    print(_y.shape)
    

    (105, 1)

    然后你最终得到exp(x) / sum(exp(x)),它是一个常数 1。所以梯度为 0,因此没有训练。

    你可以切换到tf.nn.sigmoid

    【讨论】:

      猜你喜欢
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 2018-09-01
      • 2019-05-06
      相关资源
      最近更新 更多