1第一个代码

feed_dict :喂数据
tf.placeholder:喂参数给损失函数

import numpy as np
import tensorflow as tf 
coefficients = np.array([[1.],[-10.],[20.]])
w = tf.Variable(0,dtype=tf.float32)
x = tf.placeholder(tf.float32,shape=[3,1])
cost = x[0,0]*w**2 + x[1,0]*w + x[2,0]
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
for i in range(100):
    session.run(train,feed_dict={x:coefficients})
    print(session.run(w))

你可能感兴趣的:(1第一个代码)