可以在下面的代码中帮助我吗? 尝试获取Tensorflow中的Output中的nan值 张量流会话中w和b的分布
import numpy as np
import tensorflow as tf
import numpy.random as rand
trainX = np.array([[2.5,5.6,7.8,8.9]],dtype=np.float32)
trainY = np.array([[6.7,6.7,8.9,5.4]],dtype=np.float32)
num_samples = trainX.shape[0]
num_samples2 = trainY.shape[0]
X = tf.placeholder(dtype=tf.float32)
Y = tf.placeholder(dtype=tf.float32)
type(X)
w = tf.Variable(rand.randn(1),dtype=tf.float32)
b = tf.Variable(rand.randn(1),dtype=tf.float32)
type(w)
tf.shape(w)
tf.shape(b)
w = tf.convert_to_tensor(w,dtype=tf.float32)
b = tf.convert_to_tensor(b,dtype=tf.float32)
num_iter = 10000
learning_rate = 0.01
def model(X,w,b):
return tf.multiply(X,w) + b
type(X)
type(w)
pred = model(X,w,b)
cost = tf.square(Y-pred)
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(cost)
text_file = open("Output.txt", "w")
model = tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
for i in range(num_iter):
session.run(train, feed_dict={X: trainX , Y: trainY})
print(session.run(w))
print(session.run(b))
感谢和问候, 苏波
答案 0 :(得分:2)
它是“发散的”。
降低学习率。
#learning_rate = 0.01
learning_rate = 0.001
我确认了以下结果。
[0.00044938]
[6.922184]
答案 1 :(得分:1)
一种方法是使用 tf.clip_by_value
如果费用是这样计算的
cost = -tf.reduce_sum(Y*tf.log(tf.clip_by_value(pred,1e-10,1.0)))
您将获得一个权重值。
以下代码基本上是您的代码。我试图从可训练变量列表中获取权重。您也会有偏见。
import numpy as np
import tensorflow as tf
import numpy.random as rand
trainX = np.array([[2.5,5.6,7.8,8.9]],dtype=np.float32)
trainY = np.array([[6.7,6.7,8.9,5.4]],dtype=np.float32)
num_samples = trainX.shape[0]
num_samples2 = trainY.shape[0]
X = tf.placeholder(dtype=tf.float64)
Y = tf.placeholder(dtype=tf.float64)
w = tf.get_variable(initializer=rand.randn(1),dtype=tf.float64,name="weights")
b = tf.Variable(rand.randn(1),dtype=tf.float64,name="bias")
num_iter = 10000
learning_rate = 0.01
def model(X,w,b):
return tf.multiply(X,w) + b
pred = model(X,w,b)
cost = -tf.reduce_sum(Y*tf.log(tf.clip_by_value(pred,1e-10,1.0)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(cost)
model = tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
for i in range(num_iter):
session.run(train, feed_dict={X: trainX , Y: trainY})
var = [v for v in tf.trainable_variables() if v.name == "weights:0"]
#Both print the value
print(session.run(var))
print(session.run(w))
#print(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES))