In [1]:
import tensorflow as tf
import numpy as np

In [3]:
def onehot(vocab_size, idx):
    x = np.zeros(shape=vocab_size)
    x[idx] = 1.
    return x


# if __name__ == '__main__':
with open('/home/arasdar/datasets/text_data/japan.txt', 'r') as f:
    txt = f.read()

    X = []
    y = []

    char_to_idx = {char: i for i, char in enumerate(set(txt))}
    idx_to_char = {i: char for i, char in enumerate(set(txt))}

    vocab_size = len(set(txt))

    X = np.array([onehot(vocab_size, char_to_idx[x]) for x in txt])
    X = X.reshape(1, *X.shape)

    y = X[:, 1:, :]
    y_last = onehot(vocab_size, char_to_idx['.'])
    y = np.column_stack((y, y_last.reshape(1, 1, 71)))

In [38]:
batch_size = 1
step_size = 25
H = 64

words = tf.placeholder(dtype=tf.int32, shape=[batch_size, step_size])
y = tf.placeholder(dtype=tf.int32, shape=[batch_size, step_size])

lstm = tf.nn.rnn_cell.BasicLSTMCell(H)
print(lstm.state_size, lstm.dtype, lstm.output_size, lstm.input_spec, lstm.zero_state)

Why = tf.Variable(tf.random_normal(shape=[H, vocab_size], stddev=.001))
bhy = tf.Variable(tf.zeros(vocab_size))

initial_state = state = tf.zeros(shape=[batch_size, lstm.state_size[0]])
# # initial_state = state = tf.zeros(shape=[batch_size, H])
print(initial_state, state)
# loss = 0.

# for i in range(step_size):
#     h, state = lstm(words[:, i], state)
#     prob = tf.nn.softmax(tf.matmul(h, Why) + bhy)
#     loss += -tf.reduce_mean(y[:, i] * tf.log(prob))


LSTMStateTuple(c=64, h=64) None 64 None <bound method RNNCell.zero_state of <tensorflow.python.ops.rnn_cell_impl.BasicLSTMCell object at 0x7f1a8a129da0>>
Tensor("zeros_49:0", shape=(1, 64), dtype=float32) Tensor("zeros_49:0", shape=(1, 64), dtype=float32)

In [2]:
final_state = state

sess = tf.Session()
total_loss = 0.

for i in range(10000):
    if i == 0 or idx >= X.shape[0]:
        idx = 0
        np_state = sess.run(initial_state)

    np_state, seq_loss = sess.run(
        [final_state, loss],
        feed_dict={words: X[:, idx:idx + step_size, :], initial_state: np_state}
    )
    total_loss += seq_loss


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in zeros(shape, dtype, name)
   1437     try:
-> 1438       shape = tensor_shape.as_shape(shape)
   1439       output = constant(zero, shape=shape, dtype=dtype, name=name)

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in as_shape(shape)
    848   else:
--> 849     return TensorShape(shape)
    850 

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, dims)
    454         # Got a list of dimensions
--> 455         self._dims = [as_dimension(d) for d in dims_iter]
    456 

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in <listcomp>(.0)
    454         # Got a list of dimensions
--> 455         self._dims = [as_dimension(d) for d in dims_iter]
    456 

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in as_dimension(value)
    396   else:
--> 397     return Dimension(value)
    398 

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, value)
     31     else:
---> 32       self._value = int(value)
     33       if (not isinstance(value, compat.bytes_or_text_types) and

TypeError: int() argument must be a string, a bytes-like object or a number, not 'LSTMStateTuple'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-2-05640723c92a> in <module>()
     35 bhy = tf.Variable(tf.zeros(vocab_size))
     36 
---> 37 initial_state = state = tf.zeros(shape=[batch_size, lstm.state_size])
     38 loss = 0.
     39 

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in zeros(shape, dtype, name)
   1439       output = constant(zero, shape=shape, dtype=dtype, name=name)
   1440     except (TypeError, ValueError):
-> 1441       shape = ops.convert_to_tensor(shape, dtype=dtypes.int32, name="shape")
   1442       output = fill(shape, constant(zero, dtype=dtype), name=name)
   1443   assert output.dtype.base_dtype == dtype

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
    834       name=name,
    835       preferred_dtype=preferred_dtype,
--> 836       as_ref=False)
    837 
    838 

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
    924 
    925     if ret is None:
--> 926       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    927 
    928     if ret is NotImplemented:

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    227                                          as_ref=False):
    228   _ = as_ref
--> 229   return constant(v, dtype=dtype, name=name)
    230 
    231 

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
    206   tensor_value.tensor.CopyFrom(
    207       tensor_util.make_tensor_proto(
--> 208           value, dtype=dtype, shape=shape, verify_shape=verify_shape))
    209   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    210   const_tensor = g.create_op(

~/anaconda3/envs/arasdar-DL-env/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
    382     else:
    383       _AssertCompatible(values, dtype)
--> 384       nparray = np.array(values, dtype=np_dt)
    385       # check to them.
    386       # We need to pass in quantized values as tuples, so don't apply the shape

ValueError: setting an array element with a sequence.

In [ ]: