簡單的用於解決分類問題的神經網路

2021-09-25 10:38:00 字數 1674 閱讀 2658

原理可以自己去看關於深度學習的網課,然後學習一下tensorflow的使用。看懂下面這段**,你便學會了簡單的深度學習

import tensorflow as tf

import numpy as np

learning_rate =

0.001

#學習率

batch_size =

8seed =

23455

r = np.random.randomstate(seed)

#利用隨機數產生32x2的矩陣作為資料集

x = r.rand(32,

2)#如果兩個特徵的值加起來小於1,則標籤為1

y =[

[int

(x0+x1<1)

]for

(x0,x1)

in x]

#神經網路的輸入與輸出

x = tf.placeholder(tf.float32,shape=

(none,2

))w = tf.variable(tf.random_normal([2

,3],stddev=

1,mean=0)

)w1 = tf.variable(tf.random_normal([3

,1],stddev=

1,mean=0)

)y_ = tf.placeholder(tf.float32,shape=

(none,1

))y = tf.matmul(x,w)

y1 = tf.matmul(y,w1)

#損失函式與梯度下降法(此處的反向傳播用梯度下降法)

loss = tf.reduce_mean(tf.square(y1-y_)

)train_step = tf.train.gradientdescentoptimizer(learning_rate)

.minimize(loss)

#迭代次數

steps=

int(

input()

)#通過會話來進行結點中的運算

with tf.session(

)as sess:

init_op=tf.global_variables_initializer(

) sess.run(init_op)

#開始訓練

for i in

range

(steps)

: start =

(batch_size*i)

%len

(x) end = start+batch_size #因為切片範圍不包括end,因此end不用-1

print

("開始第"

,i,"躺訓練"

) sess.run(train_step,feed_dict=

) total_loss = sess.run(loss,feed_dict=

)print

('loss: '

,total_loss)

print

("得到的權重為:\n"

)print

(sess.run(w)

)print

(sess.run(w1)

)

訓練神經網路解決而分類問題

導入庫 import tensorflow as tf from numpy.random import randomstate 定義訓練資料batch的大小 batch size 8 定義神經網路的引數 w1 tf.variable tf.random normal 2,3 stddev 1,se...

簡單的神經網路解決實際問題

先描述一下我想解決的問題 檔案中儲存著乙個大約60000行的資料,有三列,分別是時間,人數,和流量需求。我需要僅僅根據著三列資料,判斷基站的開和關,如下。import numpy as np import tensorflow as tf import matplotlib.pyplot as pl...

tensorflow實戰 實現簡單的神經網路

from tensorflow.examples.tutorials.mnist import input data import tensorflow as tf mnist input data.read data sets mnist data one hot true sess tf.int...