3类
-
- hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
- cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
-
- logits = tf.matmul(X, W) + b
- cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_oneHot))
softmax交叉熵 softmax和代价函数封装到一起
import tensorflow as tf
tf.set_random_seed(777)
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", shape=[None, 4])
Y = tf.placeholder(tf.float32, shape=[None, 3])
W = tf.Variable(tf.random_normal([4, 3]), name='weight')
b = tf.Variable(tf.random_normal([3]), name='bias')
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
prediction = tf.argmax(hypothesis, axis=1)
correct_prediction = tf.equal(prediction, tf.argmax(y_data, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(5001):
cost_val, _, acc = sess.run([cost, train, accuracy], feed_dict={X: x_data, Y: y_data})
if step % 500 == 0:
print(step, cost_val, acc)
h, c, a = sess.run([hypothesis, prediction, accuracy], feed_dict={X: x_data, Y: y_data})
print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)
h1, p1 = sess.run([hypothesis, prediction], feed_dict={X: [[1, 2, 3, 4]]})
print(h1, p1)
h2, p2 = sess.run([hypothesis, prediction], feed_dict={X: [[4,1,2,3], [3,2,4,5]]})
print(h2, '\n', p2)
while True:
str = input()
try:
if str == 'exit':
break
test = list(map(float,str.split(',')))
h1, p1 = sess.run([hypothesis, prediction], feed_dict={X: [test]})
print(h1, p1)
except:
continue
7类
import tensorflow as tf, numpy
tf.set_random_seed(1)
data = numpy.loadtxt(r'G:\A_深度学习1\tensorflow\data-04-zoo.csv', delimiter=',')
x_data = data[:, :-1]
y_data = data[:, [-1]]
n_classes = 7
X, Y = tf.placeholder('float', shape=[None, 16]), tf.placeholder(tf.int32, shape=[None, 1])
Y_oneHot = tf.one_hot(Y, n_classes)
'''
[
[[1,0,0,0,0,0,0]] 0
[[0,1,0,0,0,0,0]] 1
]
处理完后是三维的,需要变成二维
'''
Y_oneHot = tf.reshape(Y_oneHot, [-1, n_classes])
'''
[
[1,0,0,0,0,0,0]
[0,1,0,0,0,0,0]
]
'''
W, b = tf.Variable(tf.random_normal([16, n_classes])), tf.Variable(tf.random_normal([n_classes]))
logits = tf.matmul(X, W) + b
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_oneHot))
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
prediction = tf.argmax(logits, 1)
correct_prediction = tf.equal(prediction, tf.argmax(Y_oneHot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(1501):
cost_val, _, acc = sess.run([cost, train, accuracy], feed_dict={X: x_data, Y: y_data})
if step % 100 == 0:
print(step, cost_val, acc)
h, c, a = sess.run([logits, prediction, accuracy], feed_dict={X: x_data, Y: y_data})
print("\nHypothesis: ", h[:3], "\nCorrect (Y): ", c[:3], "\nAccuracy: ", a)
h1, p1 = sess.run([logits, prediction],
feed_dict={X: [[1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 8, 1, 0, 1]]})
print(h1, p1)
深层神经网路
![tensorflow 多分类_第1张图片](http://img.e-com-net.com/image/info8/d9d07968249149d0949bea93d02d202c.jpg)