ierhon commited on
Commit
99918f8
·
1 Parent(s): 287bc47

Create train.py

Browse files
Files changed (1) hide show
  1. train.py +27 -0
train.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import json
3
+ from keras.optimizers import Adam, SGD
4
+ from keras.models import Sequential
5
+ from keras.layers import Embedding, Dense, Dropout, Flatten, PReLU
6
+ from keras.preprocessing.text import Tokenizer
7
+ from keras_self_attention import SeqSelfAttention, SeqWeightedAttention
8
+
9
+ with open("dataset.json", "r") as f:
10
+ dset = json.load(f)
11
+
12
+ tokenizer = Tokenizer()
13
+ tokenizer.fit_on_texts(dset)
14
+
15
+ emb_size = 128 # how big are the word vectors in the input (how much information can be fit into one word)
16
+ vocab_size = len(tokenizer.get_vocabulary())
17
+ inp_len = 10 # limit of the input length, after 10 words the
18
+
19
+ model = Sequential()
20
+ model.add(Embedding(input_dim=vocab_size, output_dim=emb_size, input_length=inp_len))
21
+ model.add(SeqSelfAttention()) # an ATTENTION LAYER makes the model LEARN the MAIN INFORMATION in the text, AND NOT the TEXT ITSELF
22
+ model.add(Flatten()) # SelfAttention and the embedding layer outputs a 2D array, it's a list of words with a list of numbers for each word
23
+ model.add(Dense(1024, activation="relu"))
24
+ model.add(Dropout(0.5)) # dropout makes ___ task harder __ removing ____ information, 0.5 means delete 50% (it resets neurons to 0 so the model will truly focus on what's important, and not learn on some data that's there by accident)
25
+ model.add(Dense(len(dset), activation="linear")) # TBH it doesn't matter that much what activation function to use, just linear does nothing at all to the output, that might be something like softmax but i'll test that later
26
+
27
+ model.save("chatbot.keras") # It's obvious what it does, saves the model to a file