• ALL >Computer and Education >courses >university courses >undergraduate courses >模式识别与人工智能 >zstu-(2021-2022)-1 >学生作业目录 >2019329621102汪子龙

  • 2019329621102汪子龙 作业7

    Abstract

    import tensorflow as tfimport numpy as np import matplotlib.pyplot as plt x_data = np.linspace(-0.5,0.5,200)[: , np.newaxis]#-0.5--0.5之间产生200个点存到后面的2.noise = np.random.normal(0 ,0.02 ,x_data.shape) y_data = np.square(x_data) +noise x= tf.placeholder(tf.float32 ,[None ,1])#不确定行和1列y= tf.placeholder(tf.float32 ,[None ,1]) #构建中间层为10的神经网络 weights = tf.Variable(tf.random_normal([1 ,10]))biases = tf.Variable(tf.zeros([1,10])) res = tf.matmu

    Words count(1165) photos count(0) videos count(0) Download files count(0)

    ————————————————
    import tensorflow as tfimport numpy as np
    import matplotlib.pyplot as plt
    x_data = np.linspace(-0.5,0.5,200)[: , np.newaxis]#-0.5--0.5之间产生200个点存到后面的2.noise = np.random.normal(0 ,0.02 ,x_data.shape)
    y_data = np.square(x_data) +noise
    x= tf.placeholder(tf.float32 ,[None ,1])#不确定行和1列y= tf.placeholder(tf.float32 ,[None ,1])
    #构建中间层为10的神经网络
    weights = tf.Variable(tf.random_normal([1 ,10]))biases = tf.Variable(tf.zeros([1,10]))
    res = tf.matmul(x , Weights) + biases
    L1= tf.nn.tanh(res)#激活函数(双曲正切函数)
    #-输入层一个神经元x
    #-输出层为y一个神经元
    Weights_out = tf.Variable(tf.random_normal([10 ,1])biases_out = tf.Variable(tf.zeros([1,1]")
    res_out = tf.matmul(L1 , Weights_out) + biases_outpredict = tf.nn.tanh(res_out)
    #------------代价函数
    loss = tf.reduce_mean(tf.squarely - predict))
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#梯度下降法训练
    #定义会话
    with tf.Session() as sess:
    sess.run(tf.global_variables_initializerO)for i in range(2000):
    sess.run(train_step, feed_dict=fx:x_data , y:y_data))
    #获取预测值
    predict_value = sess.run(predict , feed_dict=fx:x_data})
    #画图
    plt.figure()
    plt.scatter(x_data,y_data)
    plt.plot(x_data , predict_value ," r")plt.show()


    0 Responses to 2019329621102汪子龙 作业7

    To post comment, you must login!