神經網路解決二分類問題

2021-08-19 10:36:34 字數 2218 閱讀 6676

import tensorflow as tf

from numpy.random import randomstate

batch_size = 8

w1 = tf.variable(tf.random_normal([2, 3], stddev=1, seed=1))

w2 = tf.variable(tf.random_normal([3, 1], stddev=1, seed=1))

x = tf.placeholder(tf.float32, shape=(none, 2), name="x-input")

y_ = tf.placeholder(tf.float32, shape=(none, 1), name="y-input")

a = tf.matmul(x, w1)

y = tf.matmul(a, w2)

#定義損失函式和反向傳播的演算法

cross_entropy = -tf.reduce_mean(

y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))

train_step = tf.train.adadeltaoptimizer(0.001).minimize(cross_entropy)

#通過隨機數生成乙個模擬資料集

rdm = randomstate(1)

dataset_size = 128

x = rdm.rand(dataset_size, 2)

y = [[int(x1+x2 < 1)] for (x1, x2) in x]

with tf.session() as sess:

init_op = tf.global_variables_initializer()

sess.run(init_op)

print(sess.run(w1))

print(sess.run(w2))

#設定訓練的輪數

steps = 5000

for i in range(steps):

#每次選取batch_size個樣本進行訓練

start = (i * batch_size) % dataset_size

end = min(start+batch_size, dataset_size)

#通過選取的樣本訓練神經網路並更新引數

sess.run(train_step, feed_dict=)

if i % 1000 == 0:

total_cross_entropy = sess.run(cross_entropy, feed_dict=)

print("after %d training step(s), cross entropy on all data is %g" %(i, total_cross_entropy))

print(sess.run(w1))

print(sess.run(w2))

[[-0.8113182   1.4845988   0.06532937]

[-2.4427042 0.0992484 0.5912243 ]]

[[-0.8113182 ]

[ 1.4845988 ]

[ 0.06532937]]

after 0 training step(s), cross entropy on all data is 0.0677411

after 1000 training step(s), cross entropy on all data is 0.0676762

after 2000 training step(s), cross entropy on all data is 0.0676077

after 3000 training step(s), cross entropy on all data is 0.0675391

after 4000 training step(s), cross entropy on all data is 0.0674705

[[-0.812737 1.48603 0.06671807]

[-2.444031 0.10049716 0.5924467 ]]

[[-0.812682 ]

[ 1.4861072 ]

[ 0.06662364]]

tensorflow 神經網路解決二分類問題

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

基於神經網路的二分類問題

在之前的文章中,介紹了神經網路的概念和演算法思想,甚至給出了公式推導。但依然沒有掌握神經網路的精髓,於是打算進一步學習就在網上 了吳恩達大佬的 神經網路和深度學習 這門課程,覺得收穫很大。目前只學習了單個神經元的原理及應用,下面簡單總結一下。1.損失函式的定義 與之前介紹的單個神經元的工作原理不同之...

用神經網路二分類人腦與電腦

如果乙個物件a,無論與b或c分類,分類的準確率都是50 則a為乙個具有智慧型的物件。用符號表示 a,b n m k 1,0 0,1 50 50 a,c n m k 1,0 0,1 50 50 a 智慧型體 上式的 表示a和b用乙個結構為n m k的網路分類,分類準確率為50 50 表明a與b邏輯上是...