TensorFlow 邏輯回歸手寫數字識別

2021-10-02 01:54:16 字數 1896 閱讀 1657

import input_data

import cv2

import numpy as np

import tensorflow as tf

# 訓練資料來源

mnist = input_data.read_data_sets(

'./data'

, one_hot=

true

)train_data = mnist.train.images

train_lable = mnist.train.labels

test_data = mnist.test.images

test_lable = mnist.test.labels

# 先建立乙個儲存資料的框架

x = tf.placeholder(

'float',[

none

,784])

y = tf.placeholder(

'float',[

none,10

])# 初始化引數 w 和 b

w = tf.variable(tf.random_uniform(

[784,10

],-1

,1))

b = tf.variable(tf.zeros([10

]))# 選擇 softmax 分類器

h_thta = tf.nn.softmax(tf.matmul(x, w)

+ b)

# 損失函式

cost = tf.reduce_mean(

-tf.reduce_sum(y * tf.log(h_thta)

, reduction_indices=1)

)# 選擇梯度下降方法

# 給框架賦值並初始化其它資料,用批量梯度下降方法進行迭代

with tf.session(

)as sess:

sess.run(tf.global_variables_initializer())

for _ in

range

(100):

n =int(mnist.train.num_examples /

100)

for _ in

range

(n):

batch_xs, batch_ys = mnist.train.next_batch(

100)

sess.run(optimizer, feed_dict=

) sess.run(cost, feed_dict=

)print

('w:'

, w.

eval()

)print

('==='

)print

('b:'

, b.

eval()

) h_thta_test = tf.nn.softmax(tf.matmul(img_num, w)

+ b)

sess.run(h_thta_test)

print

('---'

)print

(h_thta_test.

eval()

)

Tensorflow實現邏輯回歸

import tensorflow as tf 匯入mnist資料集 from tensorflow.examples.tutorials.mnist import input data mnist input data.read data sets temp data one hot true 定...

使用tensorflow書寫邏輯回歸

mnist 讀取訓練集 mnist.train.images mnist.train.labels 讀取訓練集的長度 mnist.train.num examples 函式 tf.placeholder 必須指定資料型別,shape可以不指定,這樣就可以使用多種shape了 tf.variable ...

Tensorflow的邏輯回歸例子,詳解

這裡做了一些修改,並加入了b值,使邏輯回歸的元素完整,取得了更好的擬合效果。邏輯回歸損失函式推導過程如下 本例構造了乙個三層網路,輸入 2cell 隱藏 10cell 輸出 1cell 我對本例的理解檢視和傳統神經網路的圖形表達,均手工畫在下面,個人認為,理解了結構之後的檢視,更利於今後的深入研究和...