• 总根 >计算机与教育 >课程 >高等教育课程 >本科课程 >模式识别与人工智能 >zstu-(2021-2022)-1 >学生作业目录 >2019329621184 吴思婷

  • 作业7

    摘要

    用tensorflow编写并执行一个数据回归问题实例 import tensorflow as tf import numpy as np # y = 3x + 2 training_set_size = 1000 true_w = 3 true_b = 2 training_set_x = 10 * np.random.random_sample(training_set_size) training_set_y = true_w * training_set_x + true_b w = tf.Variable(0, dtype=tf.float64, name="w") b = tf.Variable(0, dtype=tf.float64, name="b") x = tf.placeholder(tf.float64, name="x") y = tf.placeholder(tf.float64, name="y") hypothesis_y = w * x +

    正文字数(1498) 正文图片数(1) 正文视频数(0) 下载文件数(0)

    ————————————————
    用tensorflow编写并执行一个数据回归问题实例
    import tensorflow as tf
    
    import numpy as np

    # y = 3x + 2
    training_set_size = 1000
    true_w = 3
    true_b = 2
    training_set_x = 10 * np.random.random_sample(training_set_size)
    training_set_y = true_w * training_set_x + true_b

    w = tf.Variable(0, dtype=tf.float64, name="w")
    b = tf.Variable(0, dtype=tf.float64, name="b")
    x = tf.placeholder(tf.float64, name="x")
    y = tf.placeholder(tf.float64, name="y")
    hypothesis_y = w * x + b

    squared_deltas = tf.square(hypothesis_y - y)
    loss = tf.reduce_mean(squared_deltas)

    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
    train = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    training_set = {x: training_set_x, y: training_set_y}

    with tf.Session() as sess:
    sess.run(init)
    print("starting:", "loss = ", sess.run(loss, training_set))
    for i in range(1, 1000):
    sess.run(train, training_set)
    if i % 100 == 0:
    print("training:", "W = ", sess.run(w), "b = ", sess.run(b), "loss = ", sess.run(loss, training_set))
    print("result:", "W = ", sess.run(w), "b = ", sess.run(b), "loss = ", sess.run(loss, training_set))
    print("expect:", "W = ", true_w, "b = ", true_b)


    0 Responses to 作业7

    要发布评论,您必须先 登录!