小编bot*_*tcs的帖子

scipy convolve2d 输出错误的值

这是我用来检查 convolve2d 正确性的代码

import numpy as np
from scipy.signal import convolve2d

X = np.random.randint(5, size=(10,10))
K = np.random.randint(5, size=(3,3))
print "Input's top-left corner:"
print X[:3,:3]
print 'Kernel:'
print K

print 'Hardcording the calculation of a valid convolution (top-left)'
print (X[:3,:3]*K)
print 'Sums to'
print (X[:3,:3]*K).sum()
print 'However the top-left value of the convolve2d result'
Y = convolve2d(X, K, 'valid')
print Y[0,0]
Run Code Online (Sandbox Code Playgroud)

在我的电脑上,结果如下:

Input's top-left (3x3) corner:
[[0 0 0]
 [1 1 2]
 [1 3 0]]
Kernel:
[[4 1 1]
 [0 …
Run Code Online (Sandbox Code Playgroud)

python debugging numpy convolution scipy

8
推荐指数
2
解决办法
2763
查看次数

如何在张量流中反馈RNN输出到输入

如果假设我有一个训练有素的RNN(例如语言模型),并且我想看看它自己会产生什么,我应该如何将其输出反馈给它的输入?

我阅读了以下相关问题:

理论上我很清楚,在tensorflow中我们使用截断的反向传播,所以我们必须定义我们想要"追踪"的最大步骤.我们还为批量预留了一个维度,因此如果我想训练一个正弦波,我必须[None, num_step, 1]输入输入.

以下代码有效:

tf.reset_default_graph()
n_samples=100

state_size=5

lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(state_size, forget_bias=1.)
def_x = np.sin(np.linspace(0, 10, n_samples))[None, :, None]
zero_x = np.zeros(n_samples)[None, :, None]
X = tf.placeholder_with_default(zero_x, [None, n_samples, 1])
output, last_states = tf.nn.dynamic_rnn(inputs=X, cell=lstm_cell, dtype=tf.float64)

pred = tf.contrib.layers.fully_connected(output, 1, activation_fn=tf.tanh)

Y = np.roll(def_x, 1)
loss = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)


opt = tf.train.AdamOptimizer().minimize(loss)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

# Initial state run
plt.show(plt.plot(output.eval()[0]))
plt.plot(def_x.squeeze())
plt.show(plt.plot(pred.eval().squeeze()))

steps = 1001
for i in range(steps):
    p, l, _= …
Run Code Online (Sandbox Code Playgroud)

python lstm tensorflow recurrent-neural-network

7
推荐指数
2
解决办法
6301
查看次数