def testSharing(self, use_bias):
"""Sharing is working."""
conv1 = snt.CausalConv1D(
output_channels=1,
kernel_shape=3,
stride=1,
use_bias=use_bias,
name="conv1")
x = np.random.randn(1, 5, 1)
x1 = tf.constant(x, dtype=np.float32)
x2 = tf.constant(x, dtype=np.float32)
out1 = conv1(x1)
out2 = conv1(x2)
w = np.random.randn(3, 1, 1)
weight_change_op = conv1.w.assign(w)
init_op = tf.variables_initializer(
[conv1.w, conv1.b] if use_bias else [conv1.w])
with self.test_session() as sess:
sess.run(init_op)
first_replica_out = sess.run(out1)
second_replica_out = sess.run(out2)
# Now change the weights
sess.run(weight_change_op)
first_replica_out_changed = sess.run(out1)
second_replica_out_changed = sess.run(out2)
self.assertAllClose(first_replica_out, second_replica_out)
self.assertAllClose(first_replica_out_changed, second_replica_out_changed)
评论列表
文章目录