Ex_treme's blog.

神经网络实现(多分类logistic回归模型实现)

2018/11/13 Share

改动

  • 数据集输入
  • 图模型

数据集输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class CifarData:
def __init__(self,filenames,need_shuffle):
all_data = []
all_labels = []
for filename in filenames:
data,labels = load_data(filename)
all_data.append(data)
all_labels.append(labels)
self._data = np.vstack(all_data)
self._data = self._data / 127.5 -1
self._labels = np.hstack(all_labels)
print(self._data.shape)
print(self._labels.shape)

self._num_examples = self._data.shape[0]
self._need_shuffle = need_shuffle
self._indicator = 0
if self._need_shuffle:
self._shuffle_data()

def _shuffle_data(self):
# [0,1,2,3,4,5] - > [5,3,2,4,0,1]
p = np.random.permutation(self._num_examples)
self._data = self._data[p]
self._labels = self._labels[p]

def next_batch(self, batch_size):
"""return batch_size examples as a batch."""
end_indicator = self._indicator + batch_size
if end_indicator > self._num_examples:
if self._need_shuffle:
self._shuffle_data()
self._indicator = 0
end_indicator = batch_size
else:
raise Exception("have no more examples")
if end_indicator > self._num_examples:
raise Exception("batch size is larger than all examples")
batch_data = self._data[self._indicator:end_indicator]
batch_labels = self._labels[self._indicator: end_indicator]
self._indicator = end_indicator
return batch_data, batch_labels

图模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
tf.reset_default_graph()

# [None,3072]
x = tf.placeholder(tf.float32,[None,3072])

# [None], eg:[0,5,6,3]
y = tf.placeholder(tf.int64,[None])

# [3072,10]
w = tf.get_variable('w',[x.get_shape()[-1],10],initializer=tf.random_normal_initializer(0,1))

# (10, )
b = tf.get_variable('b',[10],initializer=tf.constant_initializer(0.0))

# [None,3072]*[3072,10] = [None,10]
y_ = tf.matmul(x,w)+b

# mean square loss
"""
# api:e^x / sum(e^x)
# [[0.01, 0.9, ..., 0.03, []]
p_y = tf.nn.softmax(y_)
# 5 - > [0,0,0,0,0,1,0,0,0,0]
y_one_hot = tf.one_hot(y,10,dtype=tf.float32)
loss = tf.reduce_mean(tf.square(y_one_hot - p_y))
"""
loss = tf.losses.sparse_softmax_cross_entropy(labels=y,logits=y_)
# y_ -> softmax
# y -> one_hot
# loss = ylogy_

# indices
predict = tf.argmax(y_,1)
# [1,0,1,1,1,0]
correct_prediction = tf.equal(predict,y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float64))

with tf.name_scope('train_op'):
train_op = tf.train.AdamOptimizer(1e-3).minimize(loss)

总结

  • 机器学习与深度学习认知
  • 神经网络

神经元、二分类logistic回归、多分类logistic回归

  • 神经网络的Tensorflow实现

Tensorflow基础

神经网络的Tensorflow实现

CATALOG
  1. 1. 改动
    1. 1.1. 数据集输入
    2. 1.2. 图模型
  2. 2. 总结