• ALL >Computer and Education >courses >university courses >undergraduate courses >模式识别与人工智能 >zstu-(2021-2022)-1 >学生作业目录 >2018329621218 陈浩臻

  • 陈浩臻2018329621218 作业7

    Abstract

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt num_points = 1000 vectors_set = [] for i in range (num_points): x1 = np.random.normal (0.0, 0.6) y1 = x1*0.5+0.3+np.random.normal(0.0,0.3) vectors_set.append([x1,y1]) x_data = [v[0] for v in vectors_set] y_data = [v[1] for v in vectors_set] W = tf.Variable(tf.random_uniform([1],-1.0,1.0),name='W') b = tf.Variable(tf.zeros([1]),name='b') y = W*x_data+b loss = tf.reduce_mean(tf.square(y-y_data),name='lo

    Words count(1141) photos count(1) videos count(0) Download files count(0)

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

    num_points = 1000
    vectors_set = []
    for i in range (num_points):
    x1 = np.random.normal (0.0, 0.6)
    y1 = x1*0.5+0.3+np.random.normal(0.0,0.3)
    vectors_set.append([x1,y1])
    x_data = [v[0] for v in vectors_set]
    y_data = [v[1] for v in vectors_set]
    W = tf.Variable(tf.random_uniform([1],-1.0,1.0),name='W')
    b = tf.Variable(tf.zeros([1]),name='b')
    y = W*x_data+b
    loss = tf.reduce_mean(tf.square(y-y_data),name='loss')
    optimizer = tf.train.GradientDescentOptimizer(0.5)
    train = optimizer.minimize(loss,name='train')
    with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    for seg in range (100):
    sess.run(train)
    print ('W=', sess.run(W), 'b=', sess.run(b),'loss=', sess.run(loss))
    plt.scatter(x_data, y_data, c='r')
    plt.plot(x_data,sess.run(W)*x_data+sess.run(b))
    plt.show



    0 Responses to 陈浩臻2018329621218 作业7

    To post comment, you must login!