【发布时间】:2019-01-23 01:19:59
【问题描述】:
我正在尝试将此 tensorflow 代码重新实现到 keras 中,我注意到此处提交的其他票证与我尝试重新创建的情绪不同。目标是在多个密集层之间共享权重矩阵。
import tensorflow as tf
# define input and weight matrices
x = tf.placeholder(shape=[None, 4], dtype=tf.float32)
w1 = tf.Variable(tf.truncated_normal(stddev=.1, shape=[4, 12]),
dtype=tf.float32)
w2 = tf.Variable(tf.truncated_normal(stddev=.1, shape=[12, 2]),
dtype=tf.float32)
# neural network
hidden_1 = tf.nn.tanh(tf.matmul(x, w1))
projection = tf.matmul(hidden_1, w2)
hidden_2 = tf.nn.tanh(projection)
hidden_3 = tf.nn.tanh(tf.matmul(hidden_2, tf.transpose(w2)))
y = tf.matmul(hidden_3, tf.transpose(w1))
# loss function and optimizer
loss = tf.reduce_mean(tf.reduce_sum((x - y) * (x - y), 1))
optimize = tf.train.AdamOptimizer().minimize(loss)
init = tf.initialize_all_variables()
问题是在 keras 中重新实现这些权重层作为原始层的转置。我目前正在使用 keras 功能 API 实现自己的网络
【问题讨论】:
标签: tensorflow keras