noshot 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- noshot/__init__.py +1 -0
- noshot/data/AIDS CN NLP/AIDS/1. Implement Basic Search Strategies/(A) Breadth First Search.ipynb +112 -0
- noshot/data/AIDS CN NLP/AIDS/1. Implement Basic Search Strategies/(B) Depth First Search.ipynb +111 -0
- noshot/data/AIDS CN NLP/AIDS/1. Implement Basic Search Strategies/(C) Uniform Cost Search.ipynb +134 -0
- noshot/data/AIDS CN NLP/AIDS/1. Implement Basic Search Strategies/(D) Depth Limites Search.ipynb +115 -0
- noshot/data/AIDS CN NLP/AIDS/1. Implement Basic Search Strategies/(E) Iterative Deepening DFS.ipynb +123 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/2_ANOVA.csv +769 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/One Way ANOVA (Repeated Measure).ipynb +126 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/One Way ANOVA.ipynb +134 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/Sample 1 Way ANOVA Test.ipynb +119 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/Two Way ANOVA.ipynb +138 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/reaction_time.csv +5 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/sample_data.csv +16 -0
- noshot/data/AIDS CN NLP/AIDS/10. ANOVA/sleep_deprivation.csv +4 -0
- noshot/data/AIDS CN NLP/AIDS/11. Linear Regression/3_Linear.csv +4802 -0
- noshot/data/AIDS CN NLP/AIDS/11. Linear Regression/Linear Regression LAB.ipynb +113 -0
- noshot/data/AIDS CN NLP/AIDS/11. Linear Regression/Linear Regression New- sklearn.ipynb +118 -0
- noshot/data/AIDS CN NLP/AIDS/11. Linear Regression/Linear Regression.ipynb +148 -0
- noshot/data/AIDS CN NLP/AIDS/11. Linear Regression/house_rate.csv +22 -0
- noshot/data/AIDS CN NLP/AIDS/12. Logistic Regression/Logistic Regression New- sklearn.ipynb +128 -0
- noshot/data/AIDS CN NLP/AIDS/12. Logistic Regression/Logistic Regression.ipynb +145 -0
- noshot/data/AIDS CN NLP/AIDS/12. Logistic Regression/default.csv +1001 -0
- noshot/data/AIDS CN NLP/AIDS/12. Logistic Regression/hours_scores_records.csv +101 -0
- noshot/data/AIDS CN NLP/AIDS/2. Implement A Star And MA Star/(A) Astar.ipynb +256 -0
- noshot/data/AIDS CN NLP/AIDS/2. Implement A Star And MA Star/(B) IDAstar.ipynb +157 -0
- noshot/data/AIDS CN NLP/AIDS/2. Implement A Star And MA Star/(C) SMAstar.ipynb +178 -0
- noshot/data/AIDS CN NLP/AIDS/3. Genetic Algorithm/Genetic.ipynb +95 -0
- noshot/data/AIDS CN NLP/AIDS/4. Simulated Annealing/Simulated Annealing.ipynb +74 -0
- noshot/data/AIDS CN NLP/AIDS/4. Simulated Annealing/Sudoku Simulated Annealing.ipynb +103 -0
- noshot/data/AIDS CN NLP/AIDS/5. Alpha Beta Pruning/AlphaBetaPruning.ipynb +182 -0
- noshot/data/AIDS CN NLP/AIDS/6. Consraint Satisfaction Problems (CSP)/(A) CSP House Allocation.ipynb +120 -0
- noshot/data/AIDS CN NLP/AIDS/6. Consraint Satisfaction Problems (CSP)/(B) CSP Map Coloring.ipynb +125 -0
- noshot/data/AIDS CN NLP/AIDS/7. Random Sampling/Random Sampling.ipynb +73 -0
- noshot/data/AIDS CN NLP/AIDS/7. Random Sampling/height_weight_bmi.csv +8389 -0
- noshot/data/AIDS CN NLP/AIDS/8. Z Test/Z Test Hash Function.ipynb +141 -0
- noshot/data/AIDS CN NLP/AIDS/8. Z Test/Z Test.ipynb +151 -0
- noshot/data/AIDS CN NLP/AIDS/8. Z Test/height_weight_bmi.csv +8389 -0
- noshot/data/AIDS CN NLP/AIDS/9. T Test/1_heart.csv +304 -0
- noshot/data/AIDS CN NLP/AIDS/9. T Test/Independent T Test.ipynb +119 -0
- noshot/data/AIDS CN NLP/AIDS/9. T Test/Paired T Test.ipynb +118 -0
- noshot/data/AIDS CN NLP/AIDS/9. T Test/T Test Hash Function.ipynb +142 -0
- noshot/data/AIDS CN NLP/AIDS/9. T Test/T Test.ipynb +158 -0
- noshot/data/AIDS CN NLP/AIDS/9. T Test/height_weight_bmi.csv +8389 -0
- noshot/data/AIDS CN NLP/AIDS/9. T Test/iq_test.csv +0 -0
- noshot/data/AIDS CN NLP/AIDS/Others (AllinOne)/All In One.ipynb +4581 -0
- noshot/data/AIDS CN NLP/CN/1. Chat Application/chat.java +81 -0
- noshot/data/AIDS CN NLP/CN/1. Chat Application/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/1. Chat Application/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/10. Ethernet LAN IEEE 802.3/LAN.tcl +65 -0
- noshot/data/AIDS CN NLP/CN/10. Ethernet LAN IEEE 802.3/analysis.awk +44 -0
- noshot/data/AIDS CN NLP/CN/10. Ethernet LAN IEEE 802.3/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/10. Ethernet LAN IEEE 802.3/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/11. Wireless LAN IEEE 802.11/complexdcf.tcl +229 -0
- noshot/data/AIDS CN NLP/CN/11. Wireless LAN IEEE 802.11/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/11. Wireless LAN IEEE 802.11/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/2. File Transfer/file_to_send.txt +2 -0
- noshot/data/AIDS CN NLP/CN/2. File Transfer/filetransfer.java +119 -0
- noshot/data/AIDS CN NLP/CN/2. File Transfer/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/2. File Transfer/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/3. RMI (Remote Method Invocation)/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/3. RMI (Remote Method Invocation)/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/3. RMI (Remote Method Invocation)/rmi.java +56 -0
- noshot/data/AIDS CN NLP/CN/4. Wired Network/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/4. Wired Network/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/4. Wired Network/wired.awk +25 -0
- noshot/data/AIDS CN NLP/CN/4. Wired Network/wired.tcl +81 -0
- noshot/data/AIDS CN NLP/CN/5. Wireless Network/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/5. Wireless Network/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/5. Wireless Network/wireless.awk +27 -0
- noshot/data/AIDS CN NLP/CN/5. Wireless Network/wireless.tcl +153 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Sack And Vegas/analysis.awk +27 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Sack And Vegas/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Sack And Vegas/sack.tcl +86 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Sack And Vegas/vegas.tcl +86 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Tahoe And Reno/analysis.awk +28 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Tahoe And Reno/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Tahoe And Reno/reno.tcl +78 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Congestion Control/Tahoe And Reno/tahoe.tcl +79 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Flow Control/analysis.awk +27 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Flow Control/flow.tcl +163 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/TCP Flow Control/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/6. TCP Flow And Congestion Control/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/7. Link State And Distance Vector Routing/DV.tcl +111 -0
- noshot/data/AIDS CN NLP/CN/7. Link State And Distance Vector Routing/LS.tcl +106 -0
- noshot/data/AIDS CN NLP/CN/7. Link State And Distance Vector Routing/analysis.awk +36 -0
- noshot/data/AIDS CN NLP/CN/7. Link State And Distance Vector Routing/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/7. Link State And Distance Vector Routing/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/8. Multicast And Broadcast Routing/analysis.awk +20 -0
- noshot/data/AIDS CN NLP/CN/8. Multicast And Broadcast Routing/broadcast.tcl +76 -0
- noshot/data/AIDS CN NLP/CN/8. Multicast And Broadcast Routing/multicast.tcl +103 -0
- noshot/data/AIDS CN NLP/CN/8. Multicast And Broadcast Routing/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/8. Multicast And Broadcast Routing/procedure.png +0 -0
- noshot/data/AIDS CN NLP/CN/9. DHCP/DHCP.java +125 -0
- noshot/data/AIDS CN NLP/CN/9. DHCP/output.png +0 -0
- noshot/data/AIDS CN NLP/CN/9. DHCP/procedure.png +0 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/1-Prereqs.py +18 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/2-Chi2test.py +83 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/2-T-test.py +79 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/3-WSD-nb.py +53 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/4-Hindle-Rooth.py +53 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/5-HMM-Trellis.py +82 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/6-HMM-Viterbi.py +16 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/7-PCFG-parsetree.py +15 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/Chi2test.ipynb +285 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/Hindle-Rooth.ipynb +179 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/Lab 10 - Text generator using LSTM.ipynb +1461 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/Lab 11 NMT.ipynb +2307 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/PCFG.ipynb +134 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/Prereqs.ipynb +131 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/T test.ipynb +252 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/TFIDF BOW.ipynb +171 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/Trellis.ipynb +244 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/WSD.ipynb +645 -0
- noshot/data/AIDS CN NLP/NLP/NLP 1/Word2Vec.ipynb +93 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab01(tokenizer)/tokenizer.ipynb +370 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab01(tokenizer)/training_tokenizer.txt +6 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab02(stemming)/exp0.ipynb +274 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab02(stemming)/lab2.ipynb +905 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab02(stemming)/test.txt +1 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab02(stemming)/tokenizing.ipynb +272 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab03(parse-tree)/collocation.ipynb +332 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab03(parse-tree)/lab3.ipynb +549 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab03(parse-tree)/nlp.txt +1 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab04(collocation)/Lab4-NLP-Exp-2.ipynb +817 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab04(collocation)/collocation.ipynb +332 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab05(WSD)/NLP-Lab-5-Exp3.ipynb +231 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab05(WSD)/word-sense-disambiguation.ipynb +507 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab06(additional-exercise)/lab6.ipynb +134 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab07(HMM,Viterbi)/NLP Exp 4.ipynb +255 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab07(HMM,Viterbi)/NLP_Exp_5.ipynb +159 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab08(PCFG)/PCFG.ipynb +282 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab09-Hindle-rooth&MLP/Lab 9 - MLP classifier.ipynb +670 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab09-Hindle-rooth&MLP/MLP-alternative-code.ipynb +613 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab09-Hindle-rooth&MLP/hindle-rooth-algorithm.ipynb +74 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab10(LSTM)/Lab_10_Text_generator_using_LSTM.ipynb +480 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab11(Viterbi-PCFG,Machine-translation)/Machine-translation.ipynb +445 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab11(Viterbi-PCFG,Machine-translation)/Viterbi-PCFG.ipynb +105 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab11(Viterbi-PCFG,Machine-translation)/corpora_tools.py +87 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab11(Viterbi-PCFG,Machine-translation)/data_utils.py +11 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab11(Viterbi-PCFG,Machine-translation)/train_translator.py +83 -0
- noshot/data/AIDS CN NLP/NLP/NLP 2/Lab12(Information-Extraction)/Information_Extraction.ipynb +201 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/Backtrack-without-Verbitri.ipynb +185 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/Backward-Procedure.ipynb +597 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/Bag_of.ipynb +1422 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/CYK-algorithm.ipynb +1067 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/Forward-Procedure.ipynb +477 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/LSTM.ipynb +1290 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/Lab 10 - Text generator using LSTM.ipynb +1461 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/Lab 11 NMT.ipynb +2307 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/NLP-LAB-4.ipynb +216 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/NLP-LAB-5.ipynb +216 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/abc.txt +6 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/ex-1-nltk.ipynb +711 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/ex-2-nlp.ipynb +267 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/exp8&9.ipynb +305 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/hind.ipynb +287 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/lab66.ipynb +752 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/leb_3.ipynb +612 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/naive_bayes_classifier.pkl +0 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/nlp_leb_1.ipynb +3008 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/nlp_leb_2.ipynb +3095 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/nlplab-9.ipynb +295 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/nltk-ex-4.ipynb +506 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/text1.txt +48 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/text2.txt +8 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/text3.txt +48 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/translation-rnn.ipynb +812 -0
- noshot/data/AIDS CN NLP/NLP/NLP 3/word2vector.ipynb +173 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Backward Procedure Algorithm.ipynb +179 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Chi Square Collocation.ipynb +208 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Collocation (T test).ipynb +188 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Experiment 1.ipynb +437 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Forward Procedure Algorithm.ipynb +132 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Hindle Rooth.ipynb +414 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/MachineTranslation.ipynb +368 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Multi Layer Perceptron using MLPClassifier.ipynb +86 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Multi Layer Perceptron using Tensorflow.ipynb +112 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/PCFG Inside Probability.ipynb +451 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Text Generation using LSTM.ipynb +297 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Viterbi.ipynb +310 -0
- noshot/data/AIDS CN NLP/NLP/NLP 4/Word Sense Disambiguation.ipynb +335 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/10.Text Generation using LSTM.ipynb +316 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/11.Machine Translation.ipynb +868 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/2.T and Chi2 Test.ipynb +204 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/3.Word Sense Diambiguation.ipynb +234 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/4.Hinddle and Rooth.ipynb +128 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/5.Forward and Backward.ipynb +149 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/6.Viterbi.ipynb +111 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/7.PCFG Parse Tree.ipynb +134 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/7.PCFG using cyk.ipynb +101 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/8.Bag of words and TF-IDF.ipynb +310 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/9.Word2Vector.ipynb +78 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/NLP ALL In One.ipynb +2619 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/sample1.txt +15 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/sample2.txt +4 -0
- noshot/data/AIDS CN NLP/NLP/NLP 5/word2vec_model.bin +0 -0
- noshot/data/AIDS CN NLP/NLP/NLP 6/1. Tokenize, Tagging, NER, Parse Tree.ipynb +312 -0
- noshot/data/AIDS CN NLP/NLP/NLP 6/2. T Test and Chi2 Test.ipynb +185 -0
- noshot/data/AIDS CN NLP/NLP/NLP 6/3. Naive Bayes WSD.ipynb +199 -0
- noshot/data/AIDS CN NLP/NLP/NLP 6/4. Hinddle and Rooth.ipynb +151 -0
- noshot/data/AIDS CN NLP/NLP/NLP 6/5 and 6 FWD, BWD, Viterbi.ipynb +164 -0
- noshot/data/AIDS CN NLP/NLP/NLP 6/7. PCFG using CYK.ipynb +383 -0
- noshot/data/AIDS CN NLP/NLP/NLP 6/8. BOW and TF-IDF.ipynb +252 -0
- noshot/data/AIDS CN NLP/Ubuntu CN Lab.iso +0 -0
- noshot/main.py +47 -0
- noshot-0.1.0.dist-info/LICENSE.txt +21 -0
- noshot-0.1.0.dist-info/METADATA +65 -0
- noshot-0.1.0.dist-info/RECORD +210 -0
- noshot-0.1.0.dist-info/WHEEL +5 -0
- noshot-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1290 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": 1,
|
6
|
+
"id": "d5f37367-910d-4abc-9448-3f80ebc0c956",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [
|
9
|
+
{
|
10
|
+
"name": "stdout",
|
11
|
+
"output_type": "stream",
|
12
|
+
"text": [
|
13
|
+
"Requirement already satisfied: tensorflow in c:\\users\\admin\\anaconda3\\lib\\site-packages (2.16.1)\n",
|
14
|
+
"Requirement already satisfied: keras in c:\\users\\admin\\anaconda3\\lib\\site-packages (3.3.2)\n",
|
15
|
+
"Requirement already satisfied: numpy in c:\\users\\admin\\anaconda3\\lib\\site-packages (1.26.4)\n",
|
16
|
+
"Requirement already satisfied: tensorflow-intel==2.16.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow) (2.16.1)\n",
|
17
|
+
"Requirement already satisfied: absl-py>=1.0.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (2.1.0)\n",
|
18
|
+
"Requirement already satisfied: astunparse>=1.6.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (1.6.3)\n",
|
19
|
+
"Requirement already satisfied: flatbuffers>=23.5.26 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (24.3.25)\n",
|
20
|
+
"Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (0.5.4)\n",
|
21
|
+
"Requirement already satisfied: google-pasta>=0.1.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (0.2.0)\n",
|
22
|
+
"Requirement already satisfied: h5py>=3.10.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (3.11.0)\n",
|
23
|
+
"Requirement already satisfied: libclang>=13.0.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (18.1.1)\n",
|
24
|
+
"Requirement already satisfied: ml-dtypes~=0.3.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (0.3.2)\n",
|
25
|
+
"Requirement already satisfied: opt-einsum>=2.3.2 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (3.3.0)\n",
|
26
|
+
"Requirement already satisfied: packaging in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (23.1)\n",
|
27
|
+
"Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (4.25.4)\n",
|
28
|
+
"Requirement already satisfied: requests<3,>=2.21.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (2.31.0)\n",
|
29
|
+
"Requirement already satisfied: setuptools in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (68.2.2)\n",
|
30
|
+
"Requirement already satisfied: six>=1.12.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (1.16.0)\n",
|
31
|
+
"Requirement already satisfied: termcolor>=1.1.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (2.4.0)\n",
|
32
|
+
"Requirement already satisfied: typing-extensions>=3.6.6 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (4.9.0)\n",
|
33
|
+
"Requirement already satisfied: wrapt>=1.11.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (1.14.1)\n",
|
34
|
+
"Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (1.62.2)\n",
|
35
|
+
"Requirement already satisfied: tensorboard<2.17,>=2.16 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (2.16.2)\n",
|
36
|
+
"Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorflow-intel==2.16.1->tensorflow) (0.31.0)\n",
|
37
|
+
"Requirement already satisfied: rich in c:\\users\\admin\\anaconda3\\lib\\site-packages (from keras) (13.3.5)\n",
|
38
|
+
"Requirement already satisfied: namex in c:\\users\\admin\\anaconda3\\lib\\site-packages (from keras) (0.0.8)\n",
|
39
|
+
"Requirement already satisfied: optree in c:\\users\\admin\\anaconda3\\lib\\site-packages (from keras) (0.11.0)\n",
|
40
|
+
"Requirement already satisfied: markdown-it-py<3.0.0,>=2.2.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from rich->keras) (2.2.0)\n",
|
41
|
+
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from rich->keras) (2.15.1)\n",
|
42
|
+
"Requirement already satisfied: wheel<1.0,>=0.23.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from astunparse>=1.6.0->tensorflow-intel==2.16.1->tensorflow) (0.41.2)\n",
|
43
|
+
"Requirement already satisfied: mdurl~=0.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from markdown-it-py<3.0.0,>=2.2.0->rich->keras) (0.1.0)\n",
|
44
|
+
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests<3,>=2.21.0->tensorflow-intel==2.16.1->tensorflow) (2.0.4)\n",
|
45
|
+
"Requirement already satisfied: idna<4,>=2.5 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests<3,>=2.21.0->tensorflow-intel==2.16.1->tensorflow) (3.4)\n",
|
46
|
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests<3,>=2.21.0->tensorflow-intel==2.16.1->tensorflow) (2.0.7)\n",
|
47
|
+
"Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests<3,>=2.21.0->tensorflow-intel==2.16.1->tensorflow) (2024.2.2)\n",
|
48
|
+
"Requirement already satisfied: markdown>=2.6.8 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorboard<2.17,>=2.16->tensorflow-intel==2.16.1->tensorflow) (3.4.1)\n",
|
49
|
+
"Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorboard<2.17,>=2.16->tensorflow-intel==2.16.1->tensorflow) (0.7.2)\n",
|
50
|
+
"Requirement already satisfied: werkzeug>=1.0.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from tensorboard<2.17,>=2.16->tensorflow-intel==2.16.1->tensorflow) (2.2.3)\n",
|
51
|
+
"Requirement already satisfied: MarkupSafe>=2.1.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from werkzeug>=1.0.1->tensorboard<2.17,>=2.16->tensorflow-intel==2.16.1->tensorflow) (2.1.3)\n",
|
52
|
+
"Note: you may need to restart the kernel to use updated packages.\n"
|
53
|
+
]
|
54
|
+
}
|
55
|
+
],
|
56
|
+
"source": [
|
57
|
+
"pip install tensorflow keras numpy\n"
|
58
|
+
]
|
59
|
+
},
|
60
|
+
{
|
61
|
+
"cell_type": "code",
|
62
|
+
"execution_count": 3,
|
63
|
+
"id": "8f4bb095-8a80-4c94-9d96-b2c32a953bb3",
|
64
|
+
"metadata": {},
|
65
|
+
"outputs": [
|
66
|
+
{
|
67
|
+
"name": "stdout",
|
68
|
+
"output_type": "stream",
|
69
|
+
"text": [
|
70
|
+
"Epoch 1/20\n"
|
71
|
+
]
|
72
|
+
},
|
73
|
+
{
|
74
|
+
"name": "stderr",
|
75
|
+
"output_type": "stream",
|
76
|
+
"text": [
|
77
|
+
"C:\\Users\\admin\\anaconda3\\Lib\\site-packages\\keras\\src\\layers\\rnn\\rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
|
78
|
+
" super().__init__(**kwargs)\n"
|
79
|
+
]
|
80
|
+
},
|
81
|
+
{
|
82
|
+
"name": "stdout",
|
83
|
+
"output_type": "stream",
|
84
|
+
"text": [
|
85
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 76ms/step - loss: 2.9326\n",
|
86
|
+
"Epoch 2/20\n",
|
87
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 75ms/step - loss: 2.2379\n",
|
88
|
+
"Epoch 3/20\n",
|
89
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.9985\n",
|
90
|
+
"Epoch 4/20\n",
|
91
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.8285\n",
|
92
|
+
"Epoch 5/20\n",
|
93
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.6803\n",
|
94
|
+
"Epoch 6/20\n",
|
95
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.5555\n",
|
96
|
+
"Epoch 7/20\n",
|
97
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.4393\n",
|
98
|
+
"Epoch 8/20\n",
|
99
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.3217\n",
|
100
|
+
"Epoch 9/20\n",
|
101
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 77ms/step - loss: 1.2309\n",
|
102
|
+
"Epoch 10/20\n",
|
103
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.1432\n",
|
104
|
+
"Epoch 11/20\n",
|
105
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.0568\n",
|
106
|
+
"Epoch 12/20\n",
|
107
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 77ms/step - loss: 0.9911\n",
|
108
|
+
"Epoch 13/20\n",
|
109
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 77ms/step - loss: 0.9336\n",
|
110
|
+
"Epoch 14/20\n",
|
111
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 77ms/step - loss: 0.8703\n",
|
112
|
+
"Epoch 15/20\n",
|
113
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 77ms/step - loss: 0.8482\n",
|
114
|
+
"Epoch 16/20\n",
|
115
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 79ms/step - loss: 0.7805\n",
|
116
|
+
"Epoch 17/20\n",
|
117
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 79ms/step - loss: 0.7586\n",
|
118
|
+
"Epoch 18/20\n",
|
119
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 78ms/step - loss: 0.7504\n",
|
120
|
+
"Epoch 19/20\n",
|
121
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 78ms/step - loss: 0.7082\n",
|
122
|
+
"Epoch 20/20\n",
|
123
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 79ms/step - loss: 0.6857\n",
|
124
|
+
"all veziers-le-rethel had followed the funeral procession of m. badon- leremince to the grave, and the the frost might of her stold that did not care for her parents me one's the fireplace.\n",
|
125
|
+
"\n",
|
126
|
+
"and they have not from my chair to be was a strander them an example, never had been placed in my heart to time. they have not never had said my heart was suddenly a crime, ready to come out. if some time, and the selod and the stars that i suffered in the child cry-chair before they have on from my chair to be was a strander them an example, never had been placed in my heart to time. they have not never h\n"
|
127
|
+
]
|
128
|
+
}
|
129
|
+
],
|
130
|
+
"source": [
|
131
|
+
"import numpy as np\n",
|
132
|
+
"import tensorflow as tf\n",
|
133
|
+
"from tensorflow.keras.models import Sequential\n",
|
134
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
135
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
136
|
+
"\n",
|
137
|
+
"# Load and preprocess the data\n",
|
138
|
+
"file_path = 'story.txt' # Replace with your .txt file path\n",
|
139
|
+
"\n",
|
140
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
141
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
142
|
+
"\n",
|
143
|
+
"# Create character-to-index and index-to-character mappings\n",
|
144
|
+
"chars = sorted(list(set(text)))\n",
|
145
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
146
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
147
|
+
"\n",
|
148
|
+
"# Prepare the dataset\n",
|
149
|
+
"sequence_length = 100\n",
|
150
|
+
"sequences = []\n",
|
151
|
+
"next_chars = []\n",
|
152
|
+
"\n",
|
153
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
154
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
155
|
+
" next_chars.append(text[i + sequence_length])\n",
|
156
|
+
"\n",
|
157
|
+
"# Convert sequences to integer format\n",
|
158
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
159
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
160
|
+
"\n",
|
161
|
+
"for i, sequence in enumerate(sequences):\n",
|
162
|
+
" for t, char in enumerate(sequence):\n",
|
163
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
164
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
165
|
+
"\n",
|
166
|
+
"# Build the LSTM model\n",
|
167
|
+
"model = Sequential()\n",
|
168
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
169
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
170
|
+
"\n",
|
171
|
+
"# Compile the model\n",
|
172
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
173
|
+
"\n",
|
174
|
+
"# Train the model\n",
|
175
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
176
|
+
"\n",
|
177
|
+
"# Function to generate text\n",
|
178
|
+
"def generate_text(seed, length):\n",
|
179
|
+
" generated = seed\n",
|
180
|
+
" for _ in range(length):\n",
|
181
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
182
|
+
" for t, char in enumerate(seed):\n",
|
183
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
184
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
185
|
+
" next_index = np.argmax(preds)\n",
|
186
|
+
" next_char = index_to_char[next_index]\n",
|
187
|
+
"\n",
|
188
|
+
" generated += next_char\n",
|
189
|
+
" seed = seed[1:] + next_char\n",
|
190
|
+
" return generated\n",
|
191
|
+
"\n",
|
192
|
+
"# Generate new text\n",
|
193
|
+
"seed_text = text[:sequence_length] # Starting with the first sequence\n",
|
194
|
+
"generated_text = generate_text(seed_text, 500) # Generate 500 characters\n",
|
195
|
+
"print(generated_text)\n"
|
196
|
+
]
|
197
|
+
},
|
198
|
+
{
|
199
|
+
"cell_type": "code",
|
200
|
+
"execution_count": 4,
|
201
|
+
"id": "08665e0a-2db2-49ea-8311-3094aaaea017",
|
202
|
+
"metadata": {},
|
203
|
+
"outputs": [
|
204
|
+
{
|
205
|
+
"name": "stdout",
|
206
|
+
"output_type": "stream",
|
207
|
+
"text": [
|
208
|
+
"Epoch 1/20\n",
|
209
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 74ms/step - loss: 3.0619\n",
|
210
|
+
"Epoch 2/20\n",
|
211
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 69ms/step - loss: 2.6110\n",
|
212
|
+
"Epoch 3/20\n",
|
213
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 2.2423\n",
|
214
|
+
"Epoch 4/20\n",
|
215
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 2.0018\n",
|
216
|
+
"Epoch 5/20\n",
|
217
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 63ms/step - loss: 1.7758\n",
|
218
|
+
"Epoch 6/20\n",
|
219
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 65ms/step - loss: 1.5593\n",
|
220
|
+
"Epoch 7/20\n",
|
221
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.3996\n",
|
222
|
+
"Epoch 8/20\n",
|
223
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 1.2476\n",
|
224
|
+
"Epoch 9/20\n",
|
225
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 1.1190\n",
|
226
|
+
"Epoch 10/20\n",
|
227
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.9928\n",
|
228
|
+
"Epoch 11/20\n",
|
229
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 66ms/step - loss: 0.9214\n",
|
230
|
+
"Epoch 12/20\n",
|
231
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 63ms/step - loss: 0.8305\n",
|
232
|
+
"Epoch 13/20\n",
|
233
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.7180\n",
|
234
|
+
"Epoch 14/20\n",
|
235
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.7005\n",
|
236
|
+
"Epoch 15/20\n",
|
237
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.6285\n",
|
238
|
+
"Epoch 16/20\n",
|
239
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.5890\n",
|
240
|
+
"Epoch 17/20\n",
|
241
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.5114\n",
|
242
|
+
"Epoch 18/20\n",
|
243
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.4845\n",
|
244
|
+
"Epoch 19/20\n",
|
245
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.4621\n",
|
246
|
+
"Epoch 20/20\n",
|
247
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.4143\n",
|
248
|
+
"the impact of artificial intelligence on data science\n",
|
249
|
+
"introduction\n",
|
250
|
+
"in recent years, the convergence of and outlial for example, ai-powered nle sensure analysis, entity to ensure manual essential for making transformed data processing capabilities, accowore morket transformed data processing capabilities, accowore morket transformed data processing capabilities, accowore morket transformed data processing capabilities, accowore morket transformed data processing capabilities, accowore morket transformed data processing capabilities, accowore morket transformed data processing capabilities, acco\n"
|
251
|
+
]
|
252
|
+
}
|
253
|
+
],
|
254
|
+
"source": [
|
255
|
+
"import numpy as np\n",
|
256
|
+
"import tensorflow as tf\n",
|
257
|
+
"from tensorflow.keras.models import Sequential\n",
|
258
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
259
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
260
|
+
"\n",
|
261
|
+
"# Load and preprocess the data\n",
|
262
|
+
"file_path = 'text3.txt' # Replace with your .txt file path\n",
|
263
|
+
"\n",
|
264
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
265
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
266
|
+
"\n",
|
267
|
+
"# Create character-to-index and index-to-character mappings\n",
|
268
|
+
"chars = sorted(list(set(text)))\n",
|
269
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
270
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
271
|
+
"\n",
|
272
|
+
"# Prepare the dataset\n",
|
273
|
+
"sequence_length = 100\n",
|
274
|
+
"sequences = []\n",
|
275
|
+
"next_chars = []\n",
|
276
|
+
"\n",
|
277
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
278
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
279
|
+
" next_chars.append(text[i + sequence_length])\n",
|
280
|
+
"\n",
|
281
|
+
"# Convert sequences to integer format\n",
|
282
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
283
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
284
|
+
"\n",
|
285
|
+
"for i, sequence in enumerate(sequences):\n",
|
286
|
+
" for t, char in enumerate(sequence):\n",
|
287
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
288
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
289
|
+
"\n",
|
290
|
+
"# Build the LSTM model\n",
|
291
|
+
"model = Sequential()\n",
|
292
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
293
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
294
|
+
"\n",
|
295
|
+
"# Compile the model\n",
|
296
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
297
|
+
"\n",
|
298
|
+
"# Train the model\n",
|
299
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
300
|
+
"\n",
|
301
|
+
"# Function to generate text\n",
|
302
|
+
"def generate_text(seed, length):\n",
|
303
|
+
" generated = seed\n",
|
304
|
+
" for _ in range(length):\n",
|
305
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
306
|
+
" for t, char in enumerate(seed):\n",
|
307
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
308
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
309
|
+
" next_index = np.argmax(preds)\n",
|
310
|
+
" next_char = index_to_char[next_index]\n",
|
311
|
+
"\n",
|
312
|
+
" generated += next_char\n",
|
313
|
+
" seed = seed[1:] + next_char\n",
|
314
|
+
" return generated\n",
|
315
|
+
"\n",
|
316
|
+
"# Generate new text\n",
|
317
|
+
"seed_text = text[:sequence_length] # Starting with the first sequence\n",
|
318
|
+
"generated_text = generate_text(seed_text, 500) # Generate 500 characters\n",
|
319
|
+
"print(generated_text)\n"
|
320
|
+
]
|
321
|
+
},
|
322
|
+
{
|
323
|
+
"cell_type": "code",
|
324
|
+
"execution_count": 5,
|
325
|
+
"id": "3417c46f-a775-4906-9df7-b4e42174499b",
|
326
|
+
"metadata": {},
|
327
|
+
"outputs": [
|
328
|
+
{
|
329
|
+
"name": "stdout",
|
330
|
+
"output_type": "stream",
|
331
|
+
"text": [
|
332
|
+
"Epoch 1/20\n",
|
333
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 64ms/step - loss: 2.9458\n",
|
334
|
+
"Epoch 2/20\n",
|
335
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 2.2906\n",
|
336
|
+
"Epoch 3/20\n",
|
337
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 2.0541\n",
|
338
|
+
"Epoch 4/20\n",
|
339
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.8668\n",
|
340
|
+
"Epoch 5/20\n",
|
341
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.7507\n",
|
342
|
+
"Epoch 6/20\n",
|
343
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.6214\n",
|
344
|
+
"Epoch 7/20\n",
|
345
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.5074\n",
|
346
|
+
"Epoch 8/20\n",
|
347
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.3756\n",
|
348
|
+
"Epoch 9/20\n",
|
349
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.2885\n",
|
350
|
+
"Epoch 10/20\n",
|
351
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.1763\n",
|
352
|
+
"Epoch 11/20\n",
|
353
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 1.1092\n",
|
354
|
+
"Epoch 12/20\n",
|
355
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.0609\n",
|
356
|
+
"Epoch 13/20\n",
|
357
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.9783\n",
|
358
|
+
"Epoch 14/20\n",
|
359
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 0.9056\n",
|
360
|
+
"Epoch 15/20\n",
|
361
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 63ms/step - loss: 0.8710\n",
|
362
|
+
"Epoch 16/20\n",
|
363
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.8234\n",
|
364
|
+
"Epoch 17/20\n",
|
365
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.8114\n",
|
366
|
+
"Epoch 18/20\n",
|
367
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.7557\n",
|
368
|
+
"Epoch 19/20\n",
|
369
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.7315\n",
|
370
|
+
"Epoch 20/20\n",
|
371
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.6878\n"
|
372
|
+
]
|
373
|
+
},
|
374
|
+
{
|
375
|
+
"name": "stdin",
|
376
|
+
"output_type": "stream",
|
377
|
+
"text": [
|
378
|
+
"Enter a seed text: My children, my dear children, I could not sleep the eternal sleep in peace if I\n"
|
379
|
+
]
|
380
|
+
},
|
381
|
+
{
|
382
|
+
"name": "stdout",
|
383
|
+
"output_type": "stream",
|
384
|
+
"text": [
|
385
|
+
"Generated Text:\n",
|
386
|
+
" my children, my dear children, i could not sleep the eternal sleep in peace if i was an hour, and saided the window the strokes mothered them they were presently deslighted her parents' consent the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child coughed again, and the child\n"
|
387
|
+
]
|
388
|
+
}
|
389
|
+
],
|
390
|
+
"source": [
|
391
|
+
"import numpy as np\n",
|
392
|
+
"import tensorflow as tf\n",
|
393
|
+
"from tensorflow.keras.models import Sequential\n",
|
394
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
395
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
396
|
+
"\n",
|
397
|
+
"# Load and preprocess the data\n",
|
398
|
+
"file_path = 'story.txt' # Replace with your .txt file path\n",
|
399
|
+
"\n",
|
400
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
401
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
402
|
+
"\n",
|
403
|
+
"# Create character-to-index and index-to-character mappings\n",
|
404
|
+
"chars = sorted(list(set(text)))\n",
|
405
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
406
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
407
|
+
"\n",
|
408
|
+
"# Prepare the dataset\n",
|
409
|
+
"sequence_length = 100\n",
|
410
|
+
"sequences = []\n",
|
411
|
+
"next_chars = []\n",
|
412
|
+
"\n",
|
413
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
414
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
415
|
+
" next_chars.append(text[i + sequence_length])\n",
|
416
|
+
"\n",
|
417
|
+
"# Convert sequences to integer format\n",
|
418
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
419
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
420
|
+
"\n",
|
421
|
+
"for i, sequence in enumerate(sequences):\n",
|
422
|
+
" for t, char in enumerate(sequence):\n",
|
423
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
424
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
425
|
+
"\n",
|
426
|
+
"# Build the LSTM model\n",
|
427
|
+
"model = Sequential()\n",
|
428
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
429
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
430
|
+
"\n",
|
431
|
+
"# Compile the model\n",
|
432
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
433
|
+
"\n",
|
434
|
+
"# Train the model\n",
|
435
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
436
|
+
"\n",
|
437
|
+
"# Function to generate text based on user input\n",
|
438
|
+
"def generate_text_based_on_input(seed, length=500):\n",
|
439
|
+
" generated = seed\n",
|
440
|
+
" for _ in range(length):\n",
|
441
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
442
|
+
" for t, char in enumerate(seed[-sequence_length:]):\n",
|
443
|
+
" if char in char_to_index:\n",
|
444
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
445
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
446
|
+
" next_index = np.argmax(preds)\n",
|
447
|
+
" next_char = index_to_char[next_index]\n",
|
448
|
+
"\n",
|
449
|
+
" generated += next_char\n",
|
450
|
+
" seed = seed[1:] + next_char\n",
|
451
|
+
" return generated\n",
|
452
|
+
"\n",
|
453
|
+
"# Get user input and generate text\n",
|
454
|
+
"user_input = input(\"Enter a seed text: \").lower() # Convert input to lowercase for consistency\n",
|
455
|
+
"if len(user_input) < sequence_length:\n",
|
456
|
+
" # If the input is too short, pad it with spaces\n",
|
457
|
+
" user_input = ' ' * (sequence_length - len(user_input)) + user_input\n",
|
458
|
+
"\n",
|
459
|
+
"generated_text = generate_text_based_on_input(user_input, 500) # Generate 500 characters\n",
|
460
|
+
"print(\"Generated Text:\\n\", generated_text)\n"
|
461
|
+
]
|
462
|
+
},
|
463
|
+
{
|
464
|
+
"cell_type": "code",
|
465
|
+
"execution_count": 6,
|
466
|
+
"id": "251b2fc3-5b54-48b0-a22c-caf834ebc71e",
|
467
|
+
"metadata": {},
|
468
|
+
"outputs": [
|
469
|
+
{
|
470
|
+
"name": "stdout",
|
471
|
+
"output_type": "stream",
|
472
|
+
"text": [
|
473
|
+
"Epoch 1/20\n",
|
474
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 62ms/step - loss: 2.9647\n",
|
475
|
+
"Epoch 2/20\n",
|
476
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 63ms/step - loss: 2.2791\n",
|
477
|
+
"Epoch 3/20\n",
|
478
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 2.0423\n",
|
479
|
+
"Epoch 4/20\n",
|
480
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.8638\n",
|
481
|
+
"Epoch 5/20\n",
|
482
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 1.7517\n",
|
483
|
+
"Epoch 6/20\n",
|
484
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 1.5960\n",
|
485
|
+
"Epoch 7/20\n",
|
486
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 1.4962\n",
|
487
|
+
"Epoch 8/20\n",
|
488
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 1.3756\n",
|
489
|
+
"Epoch 9/20\n",
|
490
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 1.2786\n",
|
491
|
+
"Epoch 10/20\n",
|
492
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.1815\n",
|
493
|
+
"Epoch 11/20\n",
|
494
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 1.0831\n",
|
495
|
+
"Epoch 12/20\n",
|
496
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.0043\n",
|
497
|
+
"Epoch 13/20\n",
|
498
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.9850\n",
|
499
|
+
"Epoch 14/20\n",
|
500
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 0.8991\n",
|
501
|
+
"Epoch 15/20\n",
|
502
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 0.8368\n",
|
503
|
+
"Epoch 16/20\n",
|
504
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 0.8337\n",
|
505
|
+
"Epoch 17/20\n",
|
506
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 62ms/step - loss: 0.7777\n",
|
507
|
+
"Epoch 18/20\n",
|
508
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.7415\n",
|
509
|
+
"Epoch 19/20\n",
|
510
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 0.7063\n",
|
511
|
+
"Epoch 20/20\n",
|
512
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.6638\n"
|
513
|
+
]
|
514
|
+
},
|
515
|
+
{
|
516
|
+
"name": "stdin",
|
517
|
+
"output_type": "stream",
|
518
|
+
"text": [
|
519
|
+
"Enter a seed text: All Veziers-le-Rethel had followed the funeral procession of M. Badon-Leremince to the grave, and the\n"
|
520
|
+
]
|
521
|
+
},
|
522
|
+
{
|
523
|
+
"name": "stdout",
|
524
|
+
"output_type": "stream",
|
525
|
+
"text": [
|
526
|
+
"Generated Text:\n",
|
527
|
+
" all veziers-le-rethel had followed the funeral procession of m. badon-leremince to the grave, and the window the destrosen an the end of a woman being the window the frightfully his spectacles with that i seemed to the end of a woman being the window the frightfully his spectacles with that i seemed to the end of a woman being the window the frightfully his spectacles with that i seemed to the end of a woman being the window the frightfully his spectacles with that i seemed to the end of a woman being the window the frightfully his spectacles with that i seemed to the end of a woman being the w\n"
|
528
|
+
]
|
529
|
+
}
|
530
|
+
],
|
531
|
+
"source": [
|
532
|
+
"import numpy as np\n",
|
533
|
+
"import tensorflow as tf\n",
|
534
|
+
"from tensorflow.keras.models import Sequential\n",
|
535
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
536
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
537
|
+
"\n",
|
538
|
+
"# Load and preprocess the data\n",
|
539
|
+
"file_path = 'story.txt' # Replace with your .txt file path\n",
|
540
|
+
"\n",
|
541
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
542
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
543
|
+
"\n",
|
544
|
+
"# Create character-to-index and index-to-character mappings\n",
|
545
|
+
"chars = sorted(list(set(text)))\n",
|
546
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
547
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
548
|
+
"\n",
|
549
|
+
"# Prepare the dataset\n",
|
550
|
+
"sequence_length = 100\n",
|
551
|
+
"sequences = []\n",
|
552
|
+
"next_chars = []\n",
|
553
|
+
"\n",
|
554
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
555
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
556
|
+
" next_chars.append(text[i + sequence_length])\n",
|
557
|
+
"\n",
|
558
|
+
"# Convert sequences to integer format\n",
|
559
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
560
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
561
|
+
"\n",
|
562
|
+
"for i, sequence in enumerate(sequences):\n",
|
563
|
+
" for t, char in enumerate(sequence):\n",
|
564
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
565
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
566
|
+
"\n",
|
567
|
+
"# Build the LSTM model\n",
|
568
|
+
"model = Sequential()\n",
|
569
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
570
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
571
|
+
"\n",
|
572
|
+
"# Compile the model\n",
|
573
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
574
|
+
"\n",
|
575
|
+
"# Train the model\n",
|
576
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
577
|
+
"\n",
|
578
|
+
"# Function to generate text based on user input\n",
|
579
|
+
"def generate_text_based_on_input(seed, length=500):\n",
|
580
|
+
" generated = seed\n",
|
581
|
+
" for _ in range(length):\n",
|
582
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
583
|
+
" for t, char in enumerate(seed[-sequence_length:]):\n",
|
584
|
+
" if char in char_to_index:\n",
|
585
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
586
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
587
|
+
" next_index = np.argmax(preds)\n",
|
588
|
+
" next_char = index_to_char[next_index]\n",
|
589
|
+
"\n",
|
590
|
+
" generated += next_char\n",
|
591
|
+
" seed = seed[1:] + next_char\n",
|
592
|
+
" return generated\n",
|
593
|
+
"\n",
|
594
|
+
"# Get user input and generate text\n",
|
595
|
+
"user_input = input(\"Enter a seed text: \").lower() # Convert input to lowercase for consistency\n",
|
596
|
+
"if len(user_input) < sequence_length:\n",
|
597
|
+
" # If the input is too short, pad it with spaces\n",
|
598
|
+
" user_input = ' ' * (sequence_length - len(user_input)) + user_input\n",
|
599
|
+
"\n",
|
600
|
+
"generated_text = generate_text_based_on_input(user_input, 500) # Generate 500 characters\n",
|
601
|
+
"print(\"Generated Text:\\n\", generated_text)\n"
|
602
|
+
]
|
603
|
+
},
|
604
|
+
{
|
605
|
+
"cell_type": "code",
|
606
|
+
"execution_count": 1,
|
607
|
+
"id": "e606230a-8322-4f7d-a1b0-953d576afd09",
|
608
|
+
"metadata": {},
|
609
|
+
"outputs": [
|
610
|
+
{
|
611
|
+
"name": "stdout",
|
612
|
+
"output_type": "stream",
|
613
|
+
"text": [
|
614
|
+
"Epoch 1/20\n"
|
615
|
+
]
|
616
|
+
},
|
617
|
+
{
|
618
|
+
"name": "stderr",
|
619
|
+
"output_type": "stream",
|
620
|
+
"text": [
|
621
|
+
"C:\\Users\\admin\\anaconda3\\Lib\\site-packages\\keras\\src\\layers\\rnn\\rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
|
622
|
+
" super().__init__(**kwargs)\n"
|
623
|
+
]
|
624
|
+
},
|
625
|
+
{
|
626
|
+
"name": "stdout",
|
627
|
+
"output_type": "stream",
|
628
|
+
"text": [
|
629
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 76ms/step - loss: 2.9391\n",
|
630
|
+
"Epoch 2/20\n",
|
631
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 2.2775\n",
|
632
|
+
"Epoch 3/20\n",
|
633
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 75ms/step - loss: 2.0429\n",
|
634
|
+
"Epoch 4/20\n",
|
635
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.8611\n",
|
636
|
+
"Epoch 5/20\n",
|
637
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 77ms/step - loss: 1.7257\n",
|
638
|
+
"Epoch 6/20\n",
|
639
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.6239\n",
|
640
|
+
"Epoch 7/20\n",
|
641
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.4916\n",
|
642
|
+
"Epoch 8/20\n",
|
643
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.3634\n",
|
644
|
+
"Epoch 9/20\n",
|
645
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 76ms/step - loss: 1.2802\n",
|
646
|
+
"Epoch 10/20\n",
|
647
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 77ms/step - loss: 1.1956\n",
|
648
|
+
"Epoch 11/20\n",
|
649
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 70ms/step - loss: 1.0818\n",
|
650
|
+
"Epoch 12/20\n",
|
651
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 63ms/step - loss: 1.0210\n",
|
652
|
+
"Epoch 13/20\n",
|
653
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 0.9462\n",
|
654
|
+
"Epoch 14/20\n",
|
655
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.9086\n",
|
656
|
+
"Epoch 15/20\n",
|
657
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.8537\n",
|
658
|
+
"Epoch 16/20\n",
|
659
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 0.7936\n",
|
660
|
+
"Epoch 17/20\n",
|
661
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.7640\n",
|
662
|
+
"Epoch 18/20\n",
|
663
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 0.7019\n",
|
664
|
+
"Epoch 19/20\n",
|
665
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.6817\n",
|
666
|
+
"Epoch 20/20\n",
|
667
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.6417\n"
|
668
|
+
]
|
669
|
+
},
|
670
|
+
{
|
671
|
+
"name": "stdin",
|
672
|
+
"output_type": "stream",
|
673
|
+
"text": [
|
674
|
+
"Enter a seed text to start generating text: All Veziers-le-Rethel had followed the funeral procession of M. Badon-Leremince to the grave, and the\n"
|
675
|
+
]
|
676
|
+
},
|
677
|
+
{
|
678
|
+
"name": "stdout",
|
679
|
+
"output_type": "stream",
|
680
|
+
"text": [
|
681
|
+
"\n",
|
682
|
+
"Generated Text:\n",
|
683
|
+
" all veziers-le-rethel had followed the funeral procession of m. badon-leremince to the grave, and the took the resartice to the windowan tound, in me or? he evering. the with her a brist for the suffering only are dang--silentus?\"\n",
|
684
|
+
"\n",
|
685
|
+
"wher it awleep if a men the tall--the likenaniso it mp, and je? he last become becoming flech.\n",
|
686
|
+
"\n",
|
687
|
+
"but more fremed at the fetter when a reast recolacked by to mad in beside my befunte from my wind; and i madt when i was enseasles with the evening. i tervouset of mournct, and shutis alone in the windration who was take.\n",
|
688
|
+
"\n",
|
689
|
+
"he wind the fetter of a gander untow at horblack.\n",
|
690
|
+
"\n"
|
691
|
+
]
|
692
|
+
}
|
693
|
+
],
|
694
|
+
"source": [
|
695
|
+
"import numpy as np\n",
|
696
|
+
"import tensorflow as tf\n",
|
697
|
+
"from tensorflow.keras.models import Sequential\n",
|
698
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
699
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
700
|
+
"\n",
|
701
|
+
"# Load and preprocess the data\n",
|
702
|
+
"file_path = 'story.txt' # Path to your story.txt\n",
|
703
|
+
"\n",
|
704
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
705
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
706
|
+
"\n",
|
707
|
+
"# Create character-to-index and index-to-character mappings\n",
|
708
|
+
"chars = sorted(list(set(text)))\n",
|
709
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
710
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
711
|
+
"\n",
|
712
|
+
"# Prepare the dataset\n",
|
713
|
+
"sequence_length = 100\n",
|
714
|
+
"sequences = []\n",
|
715
|
+
"next_chars = []\n",
|
716
|
+
"\n",
|
717
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
718
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
719
|
+
" next_chars.append(text[i + sequence_length])\n",
|
720
|
+
"\n",
|
721
|
+
"# Convert sequences to integer format\n",
|
722
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
723
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
724
|
+
"\n",
|
725
|
+
"for i, sequence in enumerate(sequences):\n",
|
726
|
+
" for t, char in enumerate(sequence):\n",
|
727
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
728
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
729
|
+
"\n",
|
730
|
+
"# Build the LSTM model\n",
|
731
|
+
"model = Sequential()\n",
|
732
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
733
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
734
|
+
"\n",
|
735
|
+
"# Compile the model\n",
|
736
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
737
|
+
"\n",
|
738
|
+
"# Train the model\n",
|
739
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
740
|
+
"\n",
|
741
|
+
"# Function to apply temperature sampling\n",
|
742
|
+
"def sample_with_temperature(preds, temperature=1.0):\n",
|
743
|
+
" preds = np.asarray(preds).astype('float64')\n",
|
744
|
+
" preds = np.log(preds + 1e-10) / temperature\n",
|
745
|
+
" exp_preds = np.exp(preds)\n",
|
746
|
+
" preds = exp_preds / np.sum(exp_preds)\n",
|
747
|
+
" probas = np.random.multinomial(1, preds, 1)\n",
|
748
|
+
" return np.argmax(probas)\n",
|
749
|
+
"\n",
|
750
|
+
"# Function to generate text based on user input with temperature\n",
|
751
|
+
"def generate_text_based_on_input(seed, length=500, temperature=1.0):\n",
|
752
|
+
" generated = seed\n",
|
753
|
+
" for _ in range(length):\n",
|
754
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
755
|
+
" for t, char in enumerate(seed[-sequence_length:]):\n",
|
756
|
+
" if char in char_to_index:\n",
|
757
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
758
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
759
|
+
" next_index = sample_with_temperature(preds, temperature)\n",
|
760
|
+
" next_char = index_to_char[next_index]\n",
|
761
|
+
"\n",
|
762
|
+
" generated += next_char\n",
|
763
|
+
" seed = seed[1:] + next_char\n",
|
764
|
+
" return generated\n",
|
765
|
+
"\n",
|
766
|
+
"# Get user input\n",
|
767
|
+
"user_input = input(\"Enter a seed text to start generating text: \").lower() # Convert to lowercase for consistency\n",
|
768
|
+
"\n",
|
769
|
+
"# Ensure the input is at least 100 characters long by padding if needed\n",
|
770
|
+
"if len(user_input) < sequence_length:\n",
|
771
|
+
" user_input = ' ' * (sequence_length - len(user_input)) + user_input\n",
|
772
|
+
"\n",
|
773
|
+
"# Generate new text based on the user's input\n",
|
774
|
+
"temperature = 0.8 # Adjust this value between 0.5 (more predictable) to 1.5 (more random)\n",
|
775
|
+
"generated_text = generate_text_based_on_input(user_input, 500, temperature=temperature) # Generate 500 characters\n",
|
776
|
+
"\n",
|
777
|
+
"print(\"\\nGenerated Text:\\n\", generated_text)\n"
|
778
|
+
]
|
779
|
+
},
|
780
|
+
{
|
781
|
+
"cell_type": "code",
|
782
|
+
"execution_count": 2,
|
783
|
+
"id": "36f0b201-9ff1-4c4d-b510-73b1952a4b55",
|
784
|
+
"metadata": {},
|
785
|
+
"outputs": [
|
786
|
+
{
|
787
|
+
"name": "stdout",
|
788
|
+
"output_type": "stream",
|
789
|
+
"text": [
|
790
|
+
"Epoch 1/20\n",
|
791
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 63ms/step - loss: 2.9254\n",
|
792
|
+
"Epoch 2/20\n",
|
793
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 2.2345\n",
|
794
|
+
"Epoch 3/20\n",
|
795
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 2.0033\n",
|
796
|
+
"Epoch 4/20\n",
|
797
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 1.8478\n",
|
798
|
+
"Epoch 5/20\n",
|
799
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 1.7384\n",
|
800
|
+
"Epoch 6/20\n",
|
801
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 63ms/step - loss: 1.6068\n",
|
802
|
+
"Epoch 7/20\n",
|
803
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 63ms/step - loss: 1.4370\n",
|
804
|
+
"Epoch 8/20\n",
|
805
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 63ms/step - loss: 1.3639\n",
|
806
|
+
"Epoch 9/20\n",
|
807
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.2820\n",
|
808
|
+
"Epoch 10/20\n",
|
809
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.1884\n",
|
810
|
+
"Epoch 11/20\n",
|
811
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.0942\n",
|
812
|
+
"Epoch 12/20\n",
|
813
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 1.0235\n",
|
814
|
+
"Epoch 13/20\n",
|
815
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.9511\n",
|
816
|
+
"Epoch 14/20\n",
|
817
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.9130\n",
|
818
|
+
"Epoch 15/20\n",
|
819
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 0.8794\n",
|
820
|
+
"Epoch 16/20\n",
|
821
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.7815\n",
|
822
|
+
"Epoch 17/20\n",
|
823
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.7733\n",
|
824
|
+
"Epoch 18/20\n",
|
825
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 60ms/step - loss: 0.7389\n",
|
826
|
+
"Epoch 19/20\n",
|
827
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 61ms/step - loss: 0.6914\n",
|
828
|
+
"Epoch 20/20\n",
|
829
|
+
"\u001b[1m104/104\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 62ms/step - loss: 0.6546\n"
|
830
|
+
]
|
831
|
+
},
|
832
|
+
{
|
833
|
+
"name": "stdin",
|
834
|
+
"output_type": "stream",
|
835
|
+
"text": [
|
836
|
+
"Enter a seed text to start generating text: every step he took, is every step closer to danger, so he said his name was peter heisenberg and asked jesse pinkman to join in in his underworld job to\n"
|
837
|
+
]
|
838
|
+
},
|
839
|
+
{
|
840
|
+
"name": "stdout",
|
841
|
+
"output_type": "stream",
|
842
|
+
"text": [
|
843
|
+
"\n",
|
844
|
+
"Generated Text:\n",
|
845
|
+
" every step he took, is every step closer to danger, so he said his name was peter heisenberg and asked jesse pinkman to join in in his underworld job to me, and i had dines spaw the stars was a that i see terrible was tounderal as in the confession of mondings the envelope.\n",
|
846
|
+
"\n",
|
847
|
+
"m. poirel de little coughs of suppace, asoral not know bean society of the surfucs of the disuracidesiling to such of his one surfession of tongs who cannot ence, who beot her parents light!\n",
|
848
|
+
"\n",
|
849
|
+
"he dat daughtar. i cannot express mell the crib.\n",
|
850
|
+
"\n",
|
851
|
+
"hew was mother lay containing to one's sillel, gone that i delt terrible calsived, and ime aw a youcted her peppanion, this was sleeps\n"
|
852
|
+
]
|
853
|
+
}
|
854
|
+
],
|
855
|
+
"source": [
|
856
|
+
"import numpy as np\n",
|
857
|
+
"import tensorflow as tf\n",
|
858
|
+
"from tensorflow.keras.models import Sequential\n",
|
859
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
860
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
861
|
+
"\n",
|
862
|
+
"# Load and preprocess the data\n",
|
863
|
+
"file_path = 'story.txt' # Path to your story.txt\n",
|
864
|
+
"\n",
|
865
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
866
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
867
|
+
"\n",
|
868
|
+
"# Create character-to-index and index-to-character mappings\n",
|
869
|
+
"chars = sorted(list(set(text)))\n",
|
870
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
871
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
872
|
+
"\n",
|
873
|
+
"# Prepare the dataset\n",
|
874
|
+
"sequence_length = 100\n",
|
875
|
+
"sequences = []\n",
|
876
|
+
"next_chars = []\n",
|
877
|
+
"\n",
|
878
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
879
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
880
|
+
" next_chars.append(text[i + sequence_length])\n",
|
881
|
+
"\n",
|
882
|
+
"# Convert sequences to integer format\n",
|
883
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
884
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
885
|
+
"\n",
|
886
|
+
"for i, sequence in enumerate(sequences):\n",
|
887
|
+
" for t, char in enumerate(sequence):\n",
|
888
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
889
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
890
|
+
"\n",
|
891
|
+
"# Build the LSTM model\n",
|
892
|
+
"model = Sequential()\n",
|
893
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
894
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
895
|
+
"\n",
|
896
|
+
"# Compile the model\n",
|
897
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
898
|
+
"\n",
|
899
|
+
"# Train the model\n",
|
900
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
901
|
+
"\n",
|
902
|
+
"# Function to apply temperature sampling\n",
|
903
|
+
"def sample_with_temperature(preds, temperature=1.0):\n",
|
904
|
+
" preds = np.asarray(preds).astype('float64')\n",
|
905
|
+
" preds = np.log(preds + 1e-10) / temperature\n",
|
906
|
+
" exp_preds = np.exp(preds)\n",
|
907
|
+
" preds = exp_preds / np.sum(exp_preds)\n",
|
908
|
+
" probas = np.random.multinomial(1, preds, 1)\n",
|
909
|
+
" return np.argmax(probas)\n",
|
910
|
+
"\n",
|
911
|
+
"# Function to generate text based on user input with temperature\n",
|
912
|
+
"def generate_text_based_on_input(seed, length=500, temperature=1.0):\n",
|
913
|
+
" generated = seed\n",
|
914
|
+
" for _ in range(length):\n",
|
915
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
916
|
+
" for t, char in enumerate(seed[-sequence_length:]):\n",
|
917
|
+
" if char in char_to_index:\n",
|
918
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
919
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
920
|
+
" next_index = sample_with_temperature(preds, temperature)\n",
|
921
|
+
" next_char = index_to_char[next_index]\n",
|
922
|
+
"\n",
|
923
|
+
" generated += next_char\n",
|
924
|
+
" seed = seed[1:] + next_char\n",
|
925
|
+
" return generated\n",
|
926
|
+
"\n",
|
927
|
+
"# Get user input\n",
|
928
|
+
"user_input = input(\"Enter a seed text to start generating text: \").lower() # Convert to lowercase for consistency\n",
|
929
|
+
"\n",
|
930
|
+
"# Ensure the input is at least 100 characters long by padding if needed\n",
|
931
|
+
"if len(user_input) < sequence_length:\n",
|
932
|
+
" user_input = ' ' * (sequence_length - len(user_input)) + user_input\n",
|
933
|
+
"\n",
|
934
|
+
"# Generate new text based on the user's input\n",
|
935
|
+
"temperature = 0.8 # Adjust this value between 0.5 (more predictable) to 1.5 (more random)\n",
|
936
|
+
"generated_text = generate_text_based_on_input(user_input, 500, temperature=temperature) # Generate 500 characters\n",
|
937
|
+
"\n",
|
938
|
+
"print(\"\\nGenerated Text:\\n\", generated_text)\n"
|
939
|
+
]
|
940
|
+
},
|
941
|
+
{
|
942
|
+
"cell_type": "code",
|
943
|
+
"execution_count": 3,
|
944
|
+
"id": "e7a5ca73-b0c1-4203-8784-29e66ac28d8f",
|
945
|
+
"metadata": {},
|
946
|
+
"outputs": [
|
947
|
+
{
|
948
|
+
"name": "stdout",
|
949
|
+
"output_type": "stream",
|
950
|
+
"text": [
|
951
|
+
"Epoch 1/20\n",
|
952
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 62ms/step - loss: 3.0079\n",
|
953
|
+
"Epoch 2/20\n",
|
954
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 2.4113\n",
|
955
|
+
"Epoch 3/20\n",
|
956
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 2.0721\n",
|
957
|
+
"Epoch 4/20\n",
|
958
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 1.7636\n",
|
959
|
+
"Epoch 5/20\n",
|
960
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.5522\n",
|
961
|
+
"Epoch 6/20\n",
|
962
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 1.3755\n",
|
963
|
+
"Epoch 7/20\n",
|
964
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.1908\n",
|
965
|
+
"Epoch 8/20\n",
|
966
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 1.0528\n",
|
967
|
+
"Epoch 9/20\n",
|
968
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 0.9187\n",
|
969
|
+
"Epoch 10/20\n",
|
970
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.8101\n",
|
971
|
+
"Epoch 11/20\n",
|
972
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.7696\n",
|
973
|
+
"Epoch 12/20\n",
|
974
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.6564\n",
|
975
|
+
"Epoch 13/20\n",
|
976
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.5807\n",
|
977
|
+
"Epoch 14/20\n",
|
978
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.5456\n",
|
979
|
+
"Epoch 15/20\n",
|
980
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.4916\n",
|
981
|
+
"Epoch 16/20\n",
|
982
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.4330\n",
|
983
|
+
"Epoch 17/20\n",
|
984
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.4294\n",
|
985
|
+
"Epoch 18/20\n",
|
986
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.3755\n",
|
987
|
+
"Epoch 19/20\n",
|
988
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.3558\n",
|
989
|
+
"Epoch 20/20\n",
|
990
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 64ms/step - loss: 0.3320\n"
|
991
|
+
]
|
992
|
+
},
|
993
|
+
{
|
994
|
+
"name": "stdin",
|
995
|
+
"output_type": "stream",
|
996
|
+
"text": [
|
997
|
+
"Enter a seed text to start generating text: AI-driven data visualization tools can automatically\n"
|
998
|
+
]
|
999
|
+
},
|
1000
|
+
{
|
1001
|
+
"name": "stdout",
|
1002
|
+
"output_type": "stream",
|
1003
|
+
"text": [
|
1004
|
+
"\n",
|
1005
|
+
"Generated Text:\n",
|
1006
|
+
" ai-driven data visualization tools can automatically generataly important that outcomes based on hast ai has tranigation.\n",
|
1007
|
+
"\n",
|
1008
|
+
"edge comons, and tagres, identify patterns. addations can the impact of ai in lea algorithms can prodising sociotes such as autofomation. techniques like straage to lights and trends intigting as, edeabled pledit or ai und sterat data science, which traising of the riskons, and preparation, advanced data visualizations based on the interactive databal complex risysns in the crakn souruted toolds and execument ai has suprers.\n",
|
1009
|
+
"\n"
|
1010
|
+
]
|
1011
|
+
}
|
1012
|
+
],
|
1013
|
+
"source": [
|
1014
|
+
"import numpy as np\n",
|
1015
|
+
"import tensorflow as tf\n",
|
1016
|
+
"from tensorflow.keras.models import Sequential\n",
|
1017
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
1018
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
1019
|
+
"\n",
|
1020
|
+
"# Load and preprocess the data\n",
|
1021
|
+
"file_path = 'text3.txt' # Path to your story.txt\n",
|
1022
|
+
"\n",
|
1023
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
1024
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
1025
|
+
"\n",
|
1026
|
+
"# Create character-to-index and index-to-character mappings\n",
|
1027
|
+
"chars = sorted(list(set(text)))\n",
|
1028
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
1029
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
1030
|
+
"\n",
|
1031
|
+
"# Prepare the dataset\n",
|
1032
|
+
"sequence_length = 100\n",
|
1033
|
+
"sequences = []\n",
|
1034
|
+
"next_chars = []\n",
|
1035
|
+
"\n",
|
1036
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
1037
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
1038
|
+
" next_chars.append(text[i + sequence_length])\n",
|
1039
|
+
"\n",
|
1040
|
+
"# Convert sequences to integer format\n",
|
1041
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
1042
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
1043
|
+
"\n",
|
1044
|
+
"for i, sequence in enumerate(sequences):\n",
|
1045
|
+
" for t, char in enumerate(sequence):\n",
|
1046
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
1047
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
1048
|
+
"\n",
|
1049
|
+
"# Build the LSTM model\n",
|
1050
|
+
"model = Sequential()\n",
|
1051
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
1052
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
1053
|
+
"\n",
|
1054
|
+
"# Compile the model\n",
|
1055
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
1056
|
+
"\n",
|
1057
|
+
"# Train the model\n",
|
1058
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
1059
|
+
"\n",
|
1060
|
+
"# Function to apply temperature sampling\n",
|
1061
|
+
"def sample_with_temperature(preds, temperature=1.0):\n",
|
1062
|
+
" preds = np.asarray(preds).astype('float64')\n",
|
1063
|
+
" preds = np.log(preds + 1e-10) / temperature\n",
|
1064
|
+
" exp_preds = np.exp(preds)\n",
|
1065
|
+
" preds = exp_preds / np.sum(exp_preds)\n",
|
1066
|
+
" probas = np.random.multinomial(1, preds, 1)\n",
|
1067
|
+
" return np.argmax(probas)\n",
|
1068
|
+
"\n",
|
1069
|
+
"# Function to generate text based on user input with temperature\n",
|
1070
|
+
"def generate_text_based_on_input(seed, length=500, temperature=1.0):\n",
|
1071
|
+
" generated = seed\n",
|
1072
|
+
" for _ in range(length):\n",
|
1073
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
1074
|
+
" for t, char in enumerate(seed[-sequence_length:]):\n",
|
1075
|
+
" if char in char_to_index:\n",
|
1076
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
1077
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
1078
|
+
" next_index = sample_with_temperature(preds, temperature)\n",
|
1079
|
+
" next_char = index_to_char[next_index]\n",
|
1080
|
+
"\n",
|
1081
|
+
" generated += next_char\n",
|
1082
|
+
" seed = seed[1:] + next_char\n",
|
1083
|
+
" return generated\n",
|
1084
|
+
"\n",
|
1085
|
+
"# Get user input\n",
|
1086
|
+
"user_input = input(\"Enter a seed text to start generating text: \").lower() # Convert to lowercase for consistency\n",
|
1087
|
+
"\n",
|
1088
|
+
"# Ensure the input is at least 100 characters long by padding if needed\n",
|
1089
|
+
"if len(user_input) < sequence_length:\n",
|
1090
|
+
" user_input = ' ' * (sequence_length - len(user_input)) + user_input\n",
|
1091
|
+
"\n",
|
1092
|
+
"# Generate new text based on the user's input\n",
|
1093
|
+
"temperature = 0.8 # Adjust this value between 0.5 (more predictable) to 1.5 (more random)\n",
|
1094
|
+
"generated_text = generate_text_based_on_input(user_input, 500, temperature=temperature) # Generate 500 characters\n",
|
1095
|
+
"\n",
|
1096
|
+
"print(\"\\nGenerated Text:\\n\", generated_text)\n"
|
1097
|
+
]
|
1098
|
+
},
|
1099
|
+
{
|
1100
|
+
"cell_type": "code",
|
1101
|
+
"execution_count": 4,
|
1102
|
+
"id": "d9f82657-ff95-41b4-a794-186864e4037f",
|
1103
|
+
"metadata": {},
|
1104
|
+
"outputs": [
|
1105
|
+
{
|
1106
|
+
"name": "stdout",
|
1107
|
+
"output_type": "stream",
|
1108
|
+
"text": [
|
1109
|
+
"Epoch 1/20\n",
|
1110
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 63ms/step - loss: 3.0312\n",
|
1111
|
+
"Epoch 2/20\n",
|
1112
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 59ms/step - loss: 2.4389\n",
|
1113
|
+
"Epoch 3/20\n",
|
1114
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 2.0864\n",
|
1115
|
+
"Epoch 4/20\n",
|
1116
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.8216\n",
|
1117
|
+
"Epoch 5/20\n",
|
1118
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.5832\n",
|
1119
|
+
"Epoch 6/20\n",
|
1120
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.3941\n",
|
1121
|
+
"Epoch 7/20\n",
|
1122
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.2256\n",
|
1123
|
+
"Epoch 8/20\n",
|
1124
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 1.0681\n",
|
1125
|
+
"Epoch 9/20\n",
|
1126
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 0.9520\n",
|
1127
|
+
"Epoch 10/20\n",
|
1128
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.8519\n",
|
1129
|
+
"Epoch 11/20\n",
|
1130
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 62ms/step - loss: 0.7699\n",
|
1131
|
+
"Epoch 12/20\n",
|
1132
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.6759\n",
|
1133
|
+
"Epoch 13/20\n",
|
1134
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 0.6146\n",
|
1135
|
+
"Epoch 14/20\n",
|
1136
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.5593\n",
|
1137
|
+
"Epoch 15/20\n",
|
1138
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 0.5197\n",
|
1139
|
+
"Epoch 16/20\n",
|
1140
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.4633\n",
|
1141
|
+
"Epoch 17/20\n",
|
1142
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 60ms/step - loss: 0.4710\n",
|
1143
|
+
"Epoch 18/20\n",
|
1144
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.3977\n",
|
1145
|
+
"Epoch 19/20\n",
|
1146
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.3992\n",
|
1147
|
+
"Epoch 20/20\n",
|
1148
|
+
"\u001b[1m62/62\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 61ms/step - loss: 0.3407\n"
|
1149
|
+
]
|
1150
|
+
},
|
1151
|
+
{
|
1152
|
+
"name": "stdin",
|
1153
|
+
"output_type": "stream",
|
1154
|
+
"text": [
|
1155
|
+
"Enter a seed text to start generating text: improve and understand\n"
|
1156
|
+
]
|
1157
|
+
},
|
1158
|
+
{
|
1159
|
+
"name": "stdout",
|
1160
|
+
"output_type": "stream",
|
1161
|
+
"text": [
|
1162
|
+
"\n",
|
1163
|
+
"Generated Text:\n",
|
1164
|
+
" improve and understand a dredve spent on action.\n",
|
1165
|
+
"\n",
|
1166
|
+
"crighes in al this. the vast improved decisions within techniques in a surtiquer innovation\n",
|
1167
|
+
"these tomly valuarly this can ai husting recolvential for making spess, aid techniquss bisting data science, which focuses on healthes and preparations. ai has transformed natural languate processing tevinion techniques can process can analyze machine learning and moces and trinssing processing inducting reclarization\n",
|
1168
|
+
"tatked techniques tht requiremention\n",
|
1169
|
+
"data visualization\n",
|
1170
|
+
"data\n"
|
1171
|
+
]
|
1172
|
+
}
|
1173
|
+
],
|
1174
|
+
"source": [
|
1175
|
+
"import numpy as np\n",
|
1176
|
+
"import tensorflow as tf\n",
|
1177
|
+
"from tensorflow.keras.models import Sequential\n",
|
1178
|
+
"from tensorflow.keras.layers import LSTM, Dense\n",
|
1179
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
1180
|
+
"\n",
|
1181
|
+
"# Load and preprocess the data\n",
|
1182
|
+
"file_path = 'text3.txt' # Path to your story.txt\n",
|
1183
|
+
"\n",
|
1184
|
+
"with open(file_path, 'r', encoding='utf-8') as file:\n",
|
1185
|
+
" text = file.read().lower() # Convert to lowercase for consistency\n",
|
1186
|
+
"\n",
|
1187
|
+
"# Create character-to-index and index-to-character mappings\n",
|
1188
|
+
"chars = sorted(list(set(text)))\n",
|
1189
|
+
"char_to_index = {c: i for i, c in enumerate(chars)}\n",
|
1190
|
+
"index_to_char = {i: c for i, c in enumerate(chars)}\n",
|
1191
|
+
"\n",
|
1192
|
+
"# Prepare the dataset\n",
|
1193
|
+
"sequence_length = 100\n",
|
1194
|
+
"sequences = []\n",
|
1195
|
+
"next_chars = []\n",
|
1196
|
+
"\n",
|
1197
|
+
"for i in range(0, len(text) - sequence_length, 1):\n",
|
1198
|
+
" sequences.append(text[i:i + sequence_length])\n",
|
1199
|
+
" next_chars.append(text[i + sequence_length])\n",
|
1200
|
+
"\n",
|
1201
|
+
"# Convert sequences to integer format\n",
|
1202
|
+
"X = np.zeros((len(sequences), sequence_length, len(chars)), dtype=bool)\n",
|
1203
|
+
"y = np.zeros((len(sequences), len(chars)), dtype=bool)\n",
|
1204
|
+
"\n",
|
1205
|
+
"for i, sequence in enumerate(sequences):\n",
|
1206
|
+
" for t, char in enumerate(sequence):\n",
|
1207
|
+
" X[i, t, char_to_index[char]] = 1\n",
|
1208
|
+
" y[i, char_to_index[next_chars[i]]] = 1\n",
|
1209
|
+
"\n",
|
1210
|
+
"# Build the LSTM model\n",
|
1211
|
+
"model = Sequential()\n",
|
1212
|
+
"model.add(LSTM(128, input_shape=(sequence_length, len(chars))))\n",
|
1213
|
+
"model.add(Dense(len(chars), activation='softmax'))\n",
|
1214
|
+
"\n",
|
1215
|
+
"# Compile the model\n",
|
1216
|
+
"model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01))\n",
|
1217
|
+
"\n",
|
1218
|
+
"# Train the model\n",
|
1219
|
+
"model.fit(X, y, batch_size=128, epochs=20)\n",
|
1220
|
+
"\n",
|
1221
|
+
"# Function to apply temperature sampling\n",
|
1222
|
+
"def sample_with_temperature(preds, temperature=1.0):\n",
|
1223
|
+
" preds = np.asarray(preds).astype('float64')\n",
|
1224
|
+
" preds = np.log(preds + 1e-10) / temperature\n",
|
1225
|
+
" exp_preds = np.exp(preds)\n",
|
1226
|
+
" preds = exp_preds / np.sum(exp_preds)\n",
|
1227
|
+
" probas = np.random.multinomial(1, preds, 1)\n",
|
1228
|
+
" return np.argmax(probas)\n",
|
1229
|
+
"\n",
|
1230
|
+
"# Function to generate text based on user input with temperature\n",
|
1231
|
+
"def generate_text_based_on_input(seed, length=500, temperature=1.0):\n",
|
1232
|
+
" generated = seed\n",
|
1233
|
+
" for _ in range(length):\n",
|
1234
|
+
" x_pred = np.zeros((1, sequence_length, len(chars)))\n",
|
1235
|
+
" for t, char in enumerate(seed[-sequence_length:]):\n",
|
1236
|
+
" if char in char_to_index:\n",
|
1237
|
+
" x_pred[0, t, char_to_index[char]] = 1\n",
|
1238
|
+
" preds = model.predict(x_pred, verbose=0)[0]\n",
|
1239
|
+
" next_index = sample_with_temperature(preds, temperature)\n",
|
1240
|
+
" next_char = index_to_char[next_index]\n",
|
1241
|
+
"\n",
|
1242
|
+
" generated += next_char\n",
|
1243
|
+
" seed = seed[1:] + next_char\n",
|
1244
|
+
" return generated\n",
|
1245
|
+
"\n",
|
1246
|
+
"# Get user input\n",
|
1247
|
+
"user_input = input(\"Enter a seed text to start generating text: \").lower() # Convert to lowercase for consistency\n",
|
1248
|
+
"\n",
|
1249
|
+
"# Ensure the input is at least 100 characters long by padding if needed\n",
|
1250
|
+
"if len(user_input) < sequence_length:\n",
|
1251
|
+
" user_input = ' ' * (sequence_length - len(user_input)) + user_input\n",
|
1252
|
+
"\n",
|
1253
|
+
"# Generate new text based on the user's input\n",
|
1254
|
+
"temperature = 0.8 # Adjust this value between 0.5 (more predictable) to 1.5 (more random)\n",
|
1255
|
+
"generated_text = generate_text_based_on_input(user_input, 500, temperature=temperature) # Generate 500 characters\n",
|
1256
|
+
"\n",
|
1257
|
+
"print(\"\\nGenerated Text:\\n\", generated_text)\n"
|
1258
|
+
]
|
1259
|
+
},
|
1260
|
+
{
|
1261
|
+
"cell_type": "code",
|
1262
|
+
"execution_count": null,
|
1263
|
+
"id": "84b8bddc-ed76-47c2-8c42-10193d55ac03",
|
1264
|
+
"metadata": {},
|
1265
|
+
"outputs": [],
|
1266
|
+
"source": []
|
1267
|
+
}
|
1268
|
+
],
|
1269
|
+
"metadata": {
|
1270
|
+
"kernelspec": {
|
1271
|
+
"display_name": "Python 3 (ipykernel)",
|
1272
|
+
"language": "python",
|
1273
|
+
"name": "python3"
|
1274
|
+
},
|
1275
|
+
"language_info": {
|
1276
|
+
"codemirror_mode": {
|
1277
|
+
"name": "ipython",
|
1278
|
+
"version": 3
|
1279
|
+
},
|
1280
|
+
"file_extension": ".py",
|
1281
|
+
"mimetype": "text/x-python",
|
1282
|
+
"name": "python",
|
1283
|
+
"nbconvert_exporter": "python",
|
1284
|
+
"pygments_lexer": "ipython3",
|
1285
|
+
"version": "3.11.7"
|
1286
|
+
}
|
1287
|
+
},
|
1288
|
+
"nbformat": 4,
|
1289
|
+
"nbformat_minor": 5
|
1290
|
+
}
|