tensorflow入門例子

2021-08-23 12:07:22 字數 931 閱讀 9453

import tensorflow as tf

import numpy as np

##使用 numpy 生成假資料(phony data), 總共 100 個點.

# (100,2)

x_data = np.float32(np.random.rand(100, 2)) # 隨機輸入

y_data = np.dot(x_data,[[0.100],[0.200]]) + 0.300

#構造乙個線性模型

# 初始化線性模型的權重,b=0,w為均勻分布

b = tf.variable(tf.zeros([1]))

w = tf.variable(tf.random_uniform([2, 1], -1.0, 1.0))

y = tf.matmul(x_data,w) + 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]

TensorFlow學習例子 1

例子 tensorflow使用的乙個小案例 原博作者注釋寫得很清晰明白。感謝。我是無理論者,並且是初接觸,打算從學習各個例子中學習熟悉tensorflow 雖然不正經,但是對前期興趣保持有好處。理解每句話 本例原理 逆概率,即 使用已有的x,y資料對和已知的模型公式,訓練出模型的引數 import ...

重要 tensorflow線性回歸例子

loss tf.reduce mean tf.square y y optimizer tf.train.gradientdescentoptimizer 0.5 線性回歸最常用的耗費函式就是mse均方誤差 from future import print function from future ...

Tensorflow中的mnist例子

git clone mnist softmax.py檔案在目錄tensorflow examples tutorials mnist 下,可以把該檔案copy出來,單獨研究 直接執行python3 mnist softmax.py,理論上應該可以直接執行,理論上 這個就只能自己想辦法了,不便多說,如...