TensorFlow學習筆記

2021-08-09 13:46:15 字數 1232 閱讀 3096

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,#*argc 解包,把陣列開啟

#te*** = train_x.shape,

train_y = 2 * train_x + np.random.randn(*train_x.shape) * 0.33 + 10

# define the model

x = tf.placeholder("float")

y = tf.placeholder("float")

w = tf.variable(0.0, name="weght")

b = tf.variable(0.0, name="bias")

loss = tf.square(y - tf.multiply(x, w) - b)

#loss = tf.square(y - tf.mul(x, w) - b)

train_op = tf.train.gradientdescentoptimizer(0.01).minimize(loss)

# create session to run

with tf.session() as sess:

sess.run(tf.initialize_all_variables())

epoch = 1

for i in range(10):

for (x, y) in zip(train_x, train_y):

_, w_value, b_value = sess.run([train_op, w, b], feed_dict=)

print("epoch: {}, w: {}, b: {}".format(epoch, w_value, b_value))

epoch += 1

除錯這段**遇到了幾個問題

*train_x.shape中 * 的意思表示將乙個陣列解壓,比如a = [1,  2,  3],那*a則是  1,  2,  3,把陣列開啟了,這麼用是因為randn函式的引數需求

tf.mul執行報錯,需要改為tf.multiply

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學習筆記

梯度下降線性回歸 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,s...