机器学习常见处理方法

img_size = mnist.train.images[0].shape[0]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import numpy as np
import tensorflow as tf
import pickle
import matplotlib.pyplot as plt

#%matplotlib inline

print("TensorFlow Version: {}".format(tf.__version__))

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('./MNIST_data/')

i = 10
img = mnist.train.images[i]
plt.imshow(img.reshape((28, 28)), cmap='Greys_r')
print("Label: {}".format(mnist.train.labels[i]))
img_size = mnist.train.images[10].shape[0]
print(img_size)
print(img.shape)

执行上述语句返回的结果为:
Label: 0
784
(784,)

hidden1 = tf.layers.dense(noise_img, n_units)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def get_generator(noise_img, n_units, out_dim, reuse=False, alpha=0.01):
"""
生成器

noise_img: 生成器的输入
n_units: 隐层单元个数
out_dim: 生成器输出tensor的size,这里应该为32*32=784
alpha: leaky ReLU系数
"""
with tf.variable_scope("generator", reuse=reuse):
# hidden layer
hidden1 = tf.layers.dense(noise_img, n_units)
# leaky ReLU
hidden1 = tf.maximum(alpha * hidden1, hidden1)
# dropout
hidden1 = tf.layers.dropout(hidden1, rate=0.2)

# logits & outputs
logits = tf.layers.dense(hidden1, out_dim)
outputs = tf.tanh(logits)

return logits, outputs

上述代码中的”hidden1 = tf.layers.dense(noise_img, n_units)
其中:
 noise_img-表示输入层的单元个数,这里使用了占位符表示,但要表明单元大小,个数为None
 n_units-表示第一个隐藏层中神经元个数,
这里的函数,表示是全连接,将输入层的神经元全部与第一个隐藏层的神经元,进行全连接。

------------------------ The End ------------------------
0%