In [1]:
import numpy as np
import tensorflow as tf
import os

In [2]:
sess = tf.Session()

In [3]:
# サンプルの画像を生成する
x_shape = [1, 4, 4, 1]
x_val = np.random.uniform(size=x_shape)

In [4]:
x_data = tf.placeholder(tf.float32, shape=x_shape)

In [5]:
my_filter = tf.constant(0.25, shape=[2, 2, 1, 1])
my_strides = [1, 2, 2, 1]

mov_avg_layer = tf.nn.conv2d(x_data, my_filter, my_strides,
                             padding='SAME', name='Moving_Avg_Window')

In [6]:
def custom_layer(input_matrix):
    input_matrix_squeezed = tf.squeeze(input_matrix)
    A = tf.constant([[1., 2.,], [-1., 3.,]])
    b = tf.constant(1., shape=[2, 2])
    temp1 = tf.matmul(A, input_matrix_squeezed)
    temp = tf.add(temp1, b)
    return (tf.nn.sigmoid(temp))

In [7]:
with tf.name_scope('Custom_Layer') as scope:
    custom_layer1 = custom_layer(mov_avg_layer)

In [8]:
print(sess.run(custom_layer1, feed_dict={x_data: x_val}))


[[ 0.9489134   0.93759835]
 [ 0.9531377   0.86491406]]

In [ ]: