python - 在 Tensor Flow 中使用带有 midi 文件的 RBM,收到一些错误

标签 python python-3.x tensorflow jupyter-notebook data-science

我正在尝试关注此笔记本,问题是它是为 Py 2.7 编写的,我正在尝试将其移植到 Py 3.6。幸运的是有人将 midi 库移植到了 Py 3 https://github.com/louisabraham/python3-midi我成功地使用它来将 midi 文件解析为一个 numpy 数组。现在我的问题是我收到了这些错误

https://github.com/bhaktipriya/Blues/blob/master/Music.ipynb


TypeError                                 Traceback (most recent call last)
<ipython-input-62-f35c20bfe55b> in <module>()
      1 #backward pass, x samples drawn from prob distribution defn by (hk,w,bv)
----> 2 x_sample=gibbs_sample(2)
      3 print(x_sample)
      4 #h sampled from prob distrib defn by (x,w,bh)
      5 h=sample(tf.sigmoid(tf.matmul(x, W) + bh))

<ipython-input-57-943cbc813622> in gibbs_sample(k)
     13     #Gibbs sample(done for k iterations) is used to approximate the distribution of the RBM(defined by W, bh, bv)
     14     ct=tf.constant(0)
---> 15     [_, _, x_sample]=control_flow_ops.while_loop(lambda count, num_iter, *args: count < num_iter,gibbs_step, [ct, tf.constant(k), x], 1, False)
     16     #to stop tensorflow from propagating gradients back through the gibbs step
     17     x_sample=tf.stop_gradient(x_sample)

c:\users\ali\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name, maximum_iterations)
   3051       raise TypeError("body must be callable.")
   3052     if parallel_iterations < 1:
-> 3053       raise TypeError("parallel_iterations must be a positive integer.")
   3054 
   3055     if maximum_iterations is not None:

TypeError: parallel_iterations must be a positive integer.

我在训练步骤中也遇到了 numpy 数组形状的奇怪错误

size_tr=tf.cast(tf.shape(x)[0], tf.float32)
eta=lr/size_tr
W_upd=tf.multiply(eta, tf.subtract(tf.matmul(tf.transpose(x), h), tf.matmul(tf.transpose(x_sample), h_sample)))
bv_upd=tf.multiply(eta, tf.reduce_sum(tf.subtract(x, x_sample), 0, True))
bh_upd=tf.multiply(eta, tf.reduce_sum(tf.subtract(h, h_sample), 0, True))
updt=[W.assign_add(W_upd), bv.assign_add(bv_upd), bh.assign_add(bh_upd)]
sess=tf.Session()
init=tf.initialize_all_variables()
sess.run(init)
for epoch in tqdm(range(epochs)):
            for song in songs:
                song=np.array(song)
                #reshaping song into chunks of timestep size
                chunks=song.shape[0]/timesteps
                chunks = int(np.floor(chunks))
                dur=chunks*timesteps
                dur = int(np.floor(dur))

                song=song[:dur]
                song=np.reshape(song, [chunks, song.shape[1]*timesteps])
                #Train the RBM on batch_size examples at a time
                for i in range(1, len(song), batch_size): 
                    tr_x=song[i:i+batch_size]
                    sess.run(updt, feed_dict={x: tr_x})
Error is:
    InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x_7' with dtype float and shape [?,2340]
         [[Node: x_7 = Placeholder[dtype=DT_FLOAT, shape=[?,2340], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

最佳答案

此错误消息表示 sess.run() 调用依赖于尚未提供的占位符。看着your code ,只有一个占位符,x。但是,错误消息中的 "_7" 表明占位符 x 已创建多次,例如通过运行多次创建它的笔记本单元格,并且它是您的图形结构中的某些内容可能取决于占位符的先前实例。例如,如果你把notebook中的一些cell乱序重新执行,就很容易出现这种情况。

您应该能够通过执行 tf.reset_default_graph() 来修复此错误然后按从上到下的顺序重新执行笔记本中的每个单元格。

关于python - 在 Tensor Flow 中使用带有 midi 文件的 RBM,收到一些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49603937/

相关文章:

python - 将时间戳舍入到最接近的 30 秒

python - 在python中生成随机时间戳列表

javascript - AngularJS:页面加载时使用服务器数据更改输入值

python - 无法将数据帧转换为 CSV

tensorflow - 对于多类问题,如何在 tensorflow 中对一类的损失进行更多的惩罚?

python - Keras:修复使用 model.fit 时的 "IndexError: list index out of range"错误

python - 比较两个文件并将缺失值写入文件

python - 在 ZOBD OOBTree 中使用对象作为键的正确方法是什么?

python - 为什么 PyCharm 中 numpy 的范围显示精度错误?

tensorflow - keras 一维卷积输入形状