Exemplo n.º 1
0
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
	with tf.name_scope(layer_name):
		with tf.name_scope(weights):
			weights = tf.Variable(tf.truncate_normal(input_dim, output_dim), stddev=0.1)
			variable_summaries(weights, layer_name + '/weights')
			
		with tf.name_scope(biases):
			biases = tf.Variable(tf.constant(0.0, shape=[output_dim]))
			variable_summaries(biases, layer_name + '/biases')
			
		with tf.name_scope('Wx_plus_b'):
			preactivate = tf.matmul(input_tensor, weights) + biases
			#记录输出节点在激活函数前的分布
			tf.summary.histogram(layer_name + '/preactivations', preactivate)
		activations = act(preactivate, name='activation')#激活函数
		#记录输出节点激活后的分布
		tf.summary.histogram(layer_name + '/activations', activations)
		return activations
Exemplo n.º 2
0
def neuron_layer(X, n_neurons, name, activation=None):
    with tf.name_scope(
            name):  # create name scope (-> will look nicer in TensorBoard)
        n_inputs = int(X.get_shape()[1])  # retrieve nbr of inputs
        stddev = 2 / np.sqrt(n_inputs)  # help faster convergence
        init = tf.truncate_normal(
            (n_inputs, n_neurons), stddev=stddev
        )  # contain weights between each input and each neuron, hence n_inputs*n_neurons
        # -> use of truncated normal ensures no large weights which could slow down the training
        W = tf.Variables(
            init,
            name="weights")  # random init. is important to break symmetry !!!
        b = tf.Variable(tf.zeros([n_neurons]), name="biases")
        z = tf.matmul(X, W) + b
        if activation == "relu":
            return tf.nn.relu(z)
        else:
            return z
Exemplo n.º 3
0
def weight_variable(shape):
    return tf.Variable(tf.truncate_normal(shape, stddev=0.1))
def weight_variable(shape):
	initial = tf.truncate_normal(shape, stddev=0.1)
	return tf.Variable(initial)
Exemplo n.º 5
0
def cnn_model(train_x, train_y, test_x, test_y, device='/cpu:0', epoch=1):
    with tf.device(device):
        x = tf.placeholder(tf.float32, [None, 48, 48, 3])
        y = tf.placeholder(tf.float32, [None, 12])
        keep_prob = tf.placeholder(tf.float32)

        conv1_w = tf.Variable(tf.truncate_normal([8, 8, 3, 8]), tf.float32)
        conv1_b = tf.Variable(tf.zeros([8]))

        conv2_w = tf.Variable(tf.truncate_normal([4, 4, 8, 16]), tf.float32)
        conv2_b = tf.Variable(tf.zeros([16]))

        conv3_w = tf.Variable(tf.truncate_normal([4, 4, 16, 32]), tf.float32)
        conv3_b = tf.Variable(tf.zeros[32])

        fc1_w = tf.Variable(tf.truncate_normal([48 * 48 * 32, 200]),
                            tf.float32)
        fc1_b = tf.Variable(tf.ones[200])

        fc2_w = tf.Variable(tf.truncate_normal([200, 200]), tf.float32)
        fc2_b = tf.Variable(tf.ones[200])

        fc3_w = tf.Variable(tf.truncate_normal([200, 12]), tf.float32)
        fc3_b = tf.Variable(tf.ones[12])

        #MODEL
        conv1 = tf.nn.relu(
            tf.nn.conv2d(x, conv1_w, strides=[1, 1, 1, 1], padding='SAME'))
        max_1 = tf.nn.max_pool(conv1,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')
        conv2 = tf.nn.relu(
            tf.nn.conv2d(max_1, conv2_w, strides=[1, 1, 1, 1], padding='SAME'))
        max_2 = tf.nn.max_pool(conv2,
                               kstrides=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')
        conv3 = tf.nn.relu(
            tf.nn.conv2d(max_2, conv3_w, stride[1, 1, 1, 1], padding='SAME'))
        max_3 = tf.nn.max_pool(conv3,
                               kstrides=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')

        reshaped = tf.reshape(max_3, [-1, 48 * 48 * 32])

        fc1 = tf.nn.dropout(tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b),
                            keep_prob)
        fc2 = tf.nn.dropout(tf.nn.relu(tf.matmul(fc1, fc2_w) + fc2_b),
                            keep_prob)
        fc3 = tf.matmul(fc2, tf3_w) + fc3_b
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=fc3))

        train = tf.train.AdamOptimizer().minimize(cross_entropy)

        cross_prediction = tf.equal(tf.argmax(fc3, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(cross_prediction, tf.float32))

    sess = tf.InteractiveSession()
    tf.initiate_global_variables().run()

    for i in range(epoch):
        if i % 100 == 0:
            print(sess.run(accuracy, feed_dict={x: x, y: y, keep_prob: 0.6}))