fork download
  1. import numpy as np
  2. import tensorflow as tf
  3. from tensorflow.keras.models import Sequential
  4. from tensorflow.keras.layers import Embedding, SimpleRNN, Dense
  5. text="This is a sample text"
  6. c=sorted(set(text))
  7. char_to_index={char:index for index,char in enumerate(c)}
  8. index_to_char={index:char for index,char in enumerate(c)}
  9. t_indices=[char_to_index[char] for char in text]
  10. seq_len,seq,n_char=20,[],[]
  11. for i in range(0,len(t_indices)-seq_len):
  12. seq.append(t_indices[i:i+seq_len])
  13. n_char.append(t_indices[i+seq_len])
  14. X,y=np.array(seq),np.array(n_char)
  15. model=Sequential([Embedding(input_dim=len(c),output_dim=50,input_length=seq_len),SimpleRNN(100,return_sequences=False),Dense(len(c),activation="softmax")])
  16. model.compile(loss="sparse_categorical_crossentropy",optimizer="adam")
  17. model.fit(X,y,batch_size=64,epochs=20)
  18. s_text="This is a sample txt"
  19. g_text=s_text
  20. num_chars_to_generate=100
  21. for _ in range(num_chars_to_generate):
  22. s_indices=[char_to_index[char] for char in s_text]
  23. if len(s_indices)<seq_len:
  24. diff=seq_len-len(s_indices)
  25. s_indices=[0]*diff+s_indices
  26. s_indices=np.array(s_indices).reshape(1,-1)
  27. n_index=model.predict(s_indices).argmax()
  28. n_char=index_to_char[n_index]
  29. g_text+=n_char
  30. s_text=s_text[1:]+n_char
  31. print(g_text)
  32.  
Success #stdin #stdout #stderr 2.28s 238320KB
stdin
Standard input is empty
stdout
Epoch 1/20

1/1 [==============================] - 0s 337ms/sample - loss: 2.3718
Epoch 2/20

1/1 [==============================] - 0s 5ms/sample - loss: 1.8814
Epoch 3/20

1/1 [==============================] - 0s 3ms/sample - loss: 1.4197
Epoch 4/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.9934
Epoch 5/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.6354
Epoch 6/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.3797
Epoch 7/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.2234
Epoch 8/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.1349
Epoch 9/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0849
Epoch 10/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0558
Epoch 11/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0384
Epoch 12/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0275
Epoch 13/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0205
Epoch 14/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0159
Epoch 15/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0126
Epoch 16/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0103
Epoch 17/20

1/1 [==============================] - 0s 4ms/sample - loss: 0.0086
Epoch 18/20

1/1 [==============================] - 0s 4ms/sample - loss: 0.0073
Epoch 19/20

1/1 [==============================] - 0s 4ms/sample - loss: 0.0063
Epoch 20/20

1/1 [==============================] - 0s 3ms/sample - loss: 0.0055
This is a sample txttaTttTtttttTttttttttattattTttattttttttttTttTttttttttttatttattattttttttttttttttTttTttttttttttatttatta
stderr
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.