<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Convolutional Weights and Bias
Weight Shape
Step1:  Bias Shape
Step2:  Convolutional Layers
Step3:  Activation Shape
Step4:  Activation2 Shape
Step5:  Fully Connected Layer
The output of a Convolutional layer must flattened to a single layer
Reshape
<ASSISTANT_TASK:>
Python Code:
# 3 x 3 filter shape
filter1 = [
    [.1, .1, .2],
    [.1, .1, .2],
    [.2, .2, .2], 
]
# Each filter only has one input channel (grey scale)
# 3 x 3 x 1
channel_filters1 = [filter1]
# We want to output 2 channels which requires another set of 3 x 3 x 1
filter2 = [
    [.9, .5, .9],
    [.5, .3, .5],
    [.9, .5, .9],
]
channel_filters2 = [filter2]
# Initialized Weights
# 3 x 3 x 1 x 2
convolution_layer1 = [channel_filters1, channel_filters2]
print(convolution_layer1[0][0][2][0])
for filters in convolution_layer1:
    for channel_filter in filters:
        for row in channel_filter:
            print(row)
        print()
Explanation: Convolutional Weights and Bias
Weight Shape: 3 x 3 x 1 x 2
* Filter sizes: 3 x 3 (Modeler's choice)
* Input Channels: 1 (Greyscale)
* Output Channels: 2 (Modeler's choice)
End of explanation
biases_1 = [0.1, 0.1]
Explanation: Bias Shape: 2
Matches the number of output channels
End of explanation
# Number of pixels to shift want evaluating a filter
stride_1 = 1
# Transpose to match inputs 
W1 = tf.Variable(np.transpose(convolution_layer1), dtype=tf.float32)
B1 = tf.Variable(biases_1, dtype=tf.float32)
print(W1.shape)
Explanation: Convolutional Layers
End of explanation
stride_shape = [1, stride_1, stride_1, 1]
preactivation = tf.nn.conv2d(X, W1, strides=stride_shape, padding='SAME') + B1
activation_1 = tf.nn.relu(preactivation)
print(activation_1.shape)
# Create a session
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
x = sess.run(W1)
# Transpose to match our model 
print(np.transpose(x))
# Transpose to match our model 
feed_dict = {X: input_x}
Y1 = activation_1.eval(session=sess, feed_dict=feed_dict)
print(np.round_(np.transpose(Y1), 1))
Explanation: Activation Shape: 4 x 4 x 2
dimension_1 = hight / stride
dimension_2 = width / stride
dimension_3 = output_channel
End of explanation
init_2 = tf.truncated_normal([4, 4, 2, 4], stddev=0.1)
W2 = tf.Variable(init_2)
B2 = tf.Variable(tf.ones([4])/10)
stride_2 = 2
strides = [1, stride_2, stride_2, 1]
preactivate = tf.nn.conv2d(activation_1, W2, strides=strides, padding='SAME') + B2
activation_2 = tf.nn.relu(preactivate)
print(activation_2.shape)
Explanation: Activation2 Shape: 2 x 2 x 4
dimension_1 = d1 / stride
dimension_2 = d2 / stride
dimension_3 = output_channel
End of explanation
# reshape the output from the third convolution for the fully connected layer
reduced = int(np.multiply.reduce(list(activation_2.shape[1:])))
re_shape = [-1, reduced]
fully_connected_input = tf.reshape(activation_2, shape=re_shape)
print(fully_connected_input.shape)
fully_connected_nodes = 6
fc_w_init = tf.truncated_normal([reduced, fully_connected_nodes], stddev=0.1)
fully_connected_weights = tf.Variable(fc_w_init)
fc_b_init = tf.ones([fully_connected_nodes])/10
fully_connected_biases = tf.Variable(fc_b_init)
preactivate = tf.matmul(fully_connected_input, fully_connected_weights) + fully_connected_biases
fully_connected_activate = tf.nn.relu(preactivate)
print(fully_connected_activate.shape)
Explanation: Fully Connected Layer
The output of a Convolutional layer must flattened to a single layer
Reshape: [2, 2, 4] --> [16]
Select a number of nodes to output like a tradional ann layer. (200)
End of explanation
<END_TASK>