enter code here
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def read_dataset():
df=pd.read_csv("D:\\data.csv")
X=df[df.columns[0]].values
Y=df[df.columns[1]].values
return X, Y
X,Y=read_dataset()
w=tf.Variable([1.0],tf.float32)
b=tf.Variable([1.0],tf.float32)
x=tf.placeholder(tf.float32)
y=tf.placeholder(tf.float32)
prediction = x*w+b
cost = tf.sigmoid(prediction)
squred_error=0.5*tf.square(y-cost)
loss=tf.reduce_sum(squred_error)
optimizer=tf.train.AdamOptimizer(0.01)
train=optimizer.minimize(loss)
init=tf.global_variables_initializer()
sess=tf.Session()
#loss=tf.Print(loss,[loss],"Loss: ")
sess.run(init)
for i in range(100):
result=sess.run(loss,feed_dict={x:X,y:Y})
#r2=sess.run(train,feed_dict={x:[1,2,3,4],y:[0,3,3,5]})
print('loss: ', result)
在运行代码之后,每次迭代我都会遭受相同的损失。
答案 0 :(得分:1)
xdurch在评论中说:您需要运行最小化操作。
_, loss_value = sess.run([train, loss], feed_dict={x:X, y:Y})