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.29s 237832KB
stdin
Standard input is empty
stdout
Epoch 1/20

1/1 [==============================] - 0s 334ms/sample - loss: 2.5327
Epoch 2/20

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

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

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

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

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

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

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

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

1/1 [==============================] - 0s 5ms/sample - loss: 0.0803
Epoch 11/20

1/1 [==============================] - 0s 6ms/sample - loss: 0.0612
Epoch 12/20

1/1 [==============================] - 0s 7ms/sample - loss: 0.0486
Epoch 13/20

1/1 [==============================] - 0s 6ms/sample - loss: 0.0392
Epoch 14/20

1/1 [==============================] - 0s 6ms/sample - loss: 0.0317
Epoch 15/20

1/1 [==============================] - 0s 6ms/sample - loss: 0.0257
Epoch 16/20

1/1 [==============================] - 0s 6ms/sample - loss: 0.0208
Epoch 17/20

1/1 [==============================] - 0s 6ms/sample - loss: 0.0169
Epoch 18/20

1/1 [==============================] - 0s 6ms/sample - loss: 0.0138
Epoch 19/20

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

1/1 [==============================] - 0s 6ms/sample - loss: 0.0094
This is a sample txttmetittitmettettitttttstettettmttittttttttttmttmttstttttttttttttmttttttttttttttttttttttttttttttttttt
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.