Demo entry 6720671

哈哈

   

Submitted by anonymous on Mar 17, 2018 at 09:05
Language: Python 3. Code size: 775 Bytes.

import tensorflow as tfimport numpy as np
# ʹÓà NumPy Éú³É¼ÙÊý¾Ý(phony data), ×ܹ² 100 ¸öµã.
x_data = np.float32(np.random.rand(2, 100)) # Ëæ»úÊäÈë
y_data = np.dot([0.100, 0.200], x_data) + 0.300
# ¹¹ÔìÒ»¸öÏßÐÔÄ£ÐÍ# 
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b
# ×îС»¯·½²î
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
# ³õʼ»¯±äÁ¿
init = tf.initialize_all_variables()
# Æô¶¯Í¼ (graph)
sess = tf.Session()
sess.run(init)
# ÄâºÏƽÃæfor step in xrange(0, 201):
    sess.run(train)
    if step % 20 == 0:
        print step, sess.run(W), sess.run(b)
# µÃµ½×î¼ÑÄâºÏ½á¹û W: [[0.100  0.200]], b: [0.300]

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).