I'm trying to implement Restricted Boltzmann model with the help of tensorflow. I'm providing input as an numpy array of size 1 X 50000.
我正在利用tensorflow实现受限的Boltzmann模型。我提供的输入是1×50000的numpy数组。
Following are the placeholders:
以下是占位符:
input_data = tf.placeholder(tf.float32, shape=[1,n_features], name="train_input")
pw = tf.placeholder(tf.float32, shape=[n_features,num_hidden], name='weights')
pbh_ = tf.placeholder(tf.float32, shape=[num_hidden],name='hidden_bias')
pbv_ = tf.placeholder(tf.float32, shape=[n_features],name='visible_bias')
When I try to run the code, I get following error:
当我试图运行代码时,我得到以下错误:
Traceback (most recent call last):
File "rbm_1.py", line 245, in <module>
tr_err = train_network()
File "rbm_1.py", line 197, in train_network
n_w = sess.run([g['w_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'train_input' with dtype float and shape [1,50000]
[[Node: train_input = Placeholder[dtype=DT_FLOAT, shape=[1,50000], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'train_input', defined at:
File "rbm_1.py", line 244, in <module>
g = struct_network()
File "rbm_1.py", line 100, in struct_network
input_data = tf.placeholder(tf.float32, shape=[1,n_features], name="train_input")
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1548, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2094, in _placeholder
name=name)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'train_input' with dtype float and shape [1,50000]
[[Node: train_input = Placeholder[dtype=DT_FLOAT, shape=[1,50000], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
I've verified that my numpy array is of dimension 1 X 50000. Have referred some older posts on stack overflow, but cannot figure the exact cause. Please help.
我已经验证了numpy数组的维数是1×50000。在堆栈溢出中引用了一些旧的帖子,但无法找到确切的原因。请帮助。
Complete code:
完整的代码:
import tensorflow as tf
import numpy as np
# for taking MFCC and label input
import rnn_input_data
import sound_constants
# for displaying elapsed time
import calendar as cal
import time
import sys
import os
# Training Parameters
num_input = 198 # mfcc data input
n_features = 50000
training_data_size = 150 # determines number of files in training and testing module
testing_data_size = num_input - training_data_size
# Network Parameters
learning_rate = 0.0001 # for large training set, it can be set 0.001
num_hidden = 300 # number of hidden layers
num_classes = 28 # total alphabet classes (a-z) + extra symbols (', ' ')
epoch = 5 # number of iterations
batch_size = 1 # number of batches
gibbs_sampling_steps = 1
#shutting down debug logs
#os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
####################################################################################
mfcc_coeffs, _ = rnn_input_data.mfcc_and_text_encoding()
class DataGenerator:
def __init__(self, data_size):
self.ptr = 0
self.epochs = 0
self.data_size = data_size
def next_batch(self):
if self.ptr > self.data_size:
self.epochs += 1
self.ptr = 0
self.ptr += batch_size
return mfcc_coeffs[ self.ptr-batch_size : self.ptr]
def sample_hidden_from_visible(visible):
hprobs = tf.nn.sigmoid(tf.add(tf.matmul((visible),w), bh_))
#hstates = tf.nn.relu(tf.sign(hprobs - hrand))
return hprobs
def sample_visible_from_hidden(num_hidden, n_features):
visible_activation = tf.add(tf.matmul(num_hidden, tf.transpose(w)), bv_)
vprobs = tf.truncated_normal((1, n_features), mean=visible_activation, stddev=0.1)
return vprobs
def gibbs_sampling_step(visible, n_features):
hprobs = sample_hidden_from_visible(visible)
vprobs = sample_visible_from_hidden(hprobs, n_features)
hprobs1 = sample_hidden_from_visible(vprobs)
return hprobs, vprobs, hprobs1
w = tf.Variable(tf.truncated_normal(shape=[n_features,num_hidden], stddev=0.1), name='weights')
bh_ = tf.Variable(tf.constant(0.1, shape=[num_hidden]),name='hidden_bias')
bv_ = tf.Variable(tf.constant(0.1, shape=[n_features]),name='visible_bias')
pw = tf.placeholder(tf.float32, shape=[n_features,num_hidden], name='weights')
pbh_ = tf.placeholder(tf.float32, shape=[num_hidden],name='hidden_bias')
pbv_ = tf.placeholder(tf.float32, shape=[n_features],name='visible_bias')
#hrand = np.random.rand(num_hidden,num_hidden)
#vrand = np.random.rand(n_features, num_hidden)
def struct_network():
input_data = tf.placeholder(tf.float32, shape=[1,n_features], name="train_input")
encode = sample_hidden_from_visible(input_data)
reconstruction = sample_visible_from_hidden(encode, n_features)
hprob0,vprob,hprob1 = gibbs_sampling_step(input_data, n_features)
positive = tf.matmul(tf.transpose(input_data), hprob0)
nn_input = vprob
for step in range(gibbs_sampling_steps - 1):
hprob,vprob, hprob1 = gibbs_sampling_step(nn_input, n_features)
nn_input = vprob
negative = tf.matmul(tf.transpose(vprob), hprob1)
w_upd8 = w + (learning_rate *(positive - negative))
bh_upd8 = bh_ + learning_rate * tf.reduce_mean(hprob0 - hprob1, 0)
bv_upd8 = bv_ + learning_rate * tf.reduce_mean(n_features - vprob, 0)
h_rand = tf.nn.sigmoid(tf.add(tf.matmul(input_data,w), bh_))
v_rand = tf.nn.sigmoid(tf.add(tf.matmul(h_rand, tf.transpose(w)),bv_))
err = input_data - v_rand
err_sum = tf.reduce_mean(err)
# returning components as dictionary elements
return {'input_data' : input_data,
'reconstruction_error':err_sum,
'w_upd8':w_upd8,
'bh_upd8':bh_upd8,
'bv_upd8':bv_upd8
}
def train_network():
with tf.Session() as sess:
train_instance = DataGenerator(training_data_size)
sess.run(tf.global_variables_initializer())
step, error = 0, 0
tr_err = []
current_epoch = 0
while current_epoch < epoch:
start_time = cal.timegm(time.gmtime())
step += 1
trb = train_instance.next_batch()
n_w = np.zeros([n_features, num_hidden], np.float32)
n_vb = np.zeros([n_features], np.float32)
n_hb = np.zeros([num_hidden], np.float32)
o_w = np.zeros([n_features, num_hidden], np.float32)
o_vb = np.zeros([n_features], np.float32)
o_hb = np.zeros([num_hidden], np.float32)
print (trb[0])
n_w = sess.run([g['w_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_hb = sess.run([g['bh_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_vb = sess.run([g['bv_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
feed = {g['input_data'] : np.transpose(trb[0])}
error_ = sess.run([g['reconstruction_error']], feed_dict=feed)[0]
error = error - error_
o_w = n_w
o_vb = n_vb
o_hb = n_hb
if train_instance.epochs > current_epoch:
current_epoch += 1
tr_err.append(error/ step)
step, error = 0, 0
return tr_err
g = struct_network()
tr_err = train_network()
1 个解决方案
#1
0
My bad. The thing I missed was I was not passing input tensor in feed_dict. Instead of passing:
我的坏。我漏掉的是我没有在feed_dict类型中传递输入张量。而不是通过:
n_w = sess.run([g['w_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
I tried passing:
我试着通过:
n_w = sess.run([g['w_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
and it worked perfectly. Corrected portions are:
而且做得很成功。纠正部分有:
n_w = sess.run([g['w_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_hb = sess.run([g['bh_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_vb = sess.run([g['bv_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
#1
0
My bad. The thing I missed was I was not passing input tensor in feed_dict. Instead of passing:
我的坏。我漏掉的是我没有在feed_dict类型中传递输入张量。而不是通过:
n_w = sess.run([g['w_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
I tried passing:
我试着通过:
n_w = sess.run([g['w_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
and it worked perfectly. Corrected portions are:
而且做得很成功。纠正部分有:
n_w = sess.run([g['w_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_hb = sess.run([g['bh_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_vb = sess.run([g['bv_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})