张量流中的线性reg

时间:2017-06-14 14:24:05

标签: python machine-learning tensorflow linear-regression

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

def generate_dataset():
    x_batch=np.linearspace(-1,1,101)
    y_batch=2*x_batch+np.random.randn(*x_batch.shape)*0.3
    return x_batch,y_batch

def linear_regression():
    x=tf.placeholder(tf.float32,shape=(None,),name='x')
    y=tf.placeholder(tf.float32,shape=(None,),name='y')
    with tf.variable_Scope('lreg') as scope:
        w=tf.Variable(np.random.normal(),name='w')
        y_pred=tf.mul(mul(w,x))
        loss=tf.reduce_mean(tf.square(y_pred-y))
    return x,y,y_pred,loss

def run():
    x_batch,y_batch=generate_dataset()
    x,y,y_pred,loss=linear_regression()
    optimizer=tf.train.GradientDescentOptimizer(.1).minimize(loss)
    init=tf.global_variables_initialzer()
    with tf.Session() as session:
        session.run(init)
        feed_dict={x:x_batch,y:y_batch}
        for _ in range(30):
            loss_val, _ =session.run([loss,optimizer],feed_dict)
            print('loss:',loss_val.mean())

我的代码问题是loss_value的值没有打印任何东西。我的代码没有显示任何错误,但没有打印。

0 个答案:

没有答案