梯度下降線性回歸
import tensorflow as tf
w = tf.variable(tf.random_normal([1]), name='weight')
b = tf.variable(tf.random_normal([1]), name='bias')
x = tf.placeholder(tf.float32, shape=[none])
y = tf.placeholder(tf.float32, shape=[none])
hypothesis = x * w + b
cost = tf.reduce_mean(tf.square(hypothesis - y))
optimizer = tf.train.gradientdescentoptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
sess = tf.session()
sess.run(tf.global_variables_initializer())
for step in range(2001):
# sess.run(train)
# b_val後面的' _ '不能少啊,對應著train
cost_val, w_val, b_val, _ = sess.run([cost, w, b, train],
feed_dict=)
if step % 20 == 0:
print(step, cost_val)
softmax
import tensorflow as tf
x_data = [[1, 2, 1, 1], [2, 1, 3, 2], [3, 1, 3, 4], [4, 1, 5, 5], [1, 7, 5, 5], [1, 2, 5, 6], [1, 6, 6, 6], [1, 7, 7, 7]]
y_data = [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]]
x = tf.placeholder("float", [none, 4])# 2維張量的形式,也就是矩陣,行數為none也就是樣本數量不受限制
y = tf.placeholder("float", [none, 3])
nb_classes = 3
# 識別的類別數
w = tf.variable(tf.random_normal([4, nb_classes], name="weight"))
b = tf.variable(tf.random_normal([nb_classes], name="bias"))# 注意這裡的bias也是每個類別乙個
hypothesis = tf.nn.softmax(tf.matmul(x, w) + b)
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(hypothesis), axis=1))
optimizer = tf.train.gradientdescentoptimizer(learning_rate=0.1).minimize(cost)
with tf.session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(2001):
sess.run(optimizer, feed_dict=)
if step%200 == 0:
簡單的說,tf.argmax就是返回最大的那個數值所在的下標。softmax實現mnist手寫數字識別a = sess.run(hypothesis, feed_dict=)
print(a, sess.run(tf.arg_max(a, 1)))
輸出[[ 1.38904958e-03 9.98601854e-01 9.06129117e-06]] [1]
1就是最大元素的下標
import tensorflow as tf
import input_data
mnist = input_data.read_data_sets('mnist_data/', one_hot=true)
x = tf.placeholder("float", [none, 784])
y_ = tf.placeholder("float", [none, 10])
nb_classes = 10
w = tf.variable(tf.random_normal([784, nb_classes], name="weight"))
b = tf.variable(tf.random_normal([nb_classes], name="bias"))
y = tf.nn.softmax(tf.matmul(x, w) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), axis=1))
optimizer = tf.train.gradientdescentoptimizer(learning_rate=0.2).minimize(cross_entropy)
with tf.session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(2000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(optimizer, feed_dict=)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))#好像直接拿的訓練集做的驗證
print(sess.run(accuracy, feed_dict=))
以及非常好的搭建cnn的教程
tensorflow學習筆記
tensorflow安裝可以直接通過命令列或者原始碼安裝,在此介紹tensorflow8命令列安裝如下 安裝tensorflow sudo pip install upgrade 另外,解除安裝tensorflow命令為 sudo pip uninstall tensorflow tensorflo...
Tensorflow學習筆記
1.如何在虛擬機器中安裝tensor flow 1 首先安裝pip pip install 2 pip install 2.學習tensorflow需要學習 python and linux 3.使用 tensorflow,你必須明白 tensorflow 1 使用圖 graph 來表示計算任務.2...
TensorFlow學習筆記
1 擬合直線 import the library import tensorflow as tf import numpy as np prepare train data train x np.linspace 1,1,100 temp1 train x,temp2 train x.shape,...