-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuntitled.py
84 lines (67 loc) · 2.83 KB
/
untitled.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""Untitled.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1gcDwD4fUxTVGnRbEmZoIYGDhCeTNYhrt
"""
import pandas as pd
df = pd.read_csv('sample_data/ramen-ratings.csv')
from keras.layers import Input, LSTM, Bidirectional, SpatialDropout1D, Dropout, Flatten, Dense, Embedding, BatchNormalization
from keras.models import Model
from keras.callbacks import EarlyStopping
from keras.preprocessing.text import Tokenizer, text_to_word_sequence
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
import nltk, os, re, string
from nltk.corpus import stopwords
Style = pd.get_dummies(df.Style)
df_baru = pd.concat([df, Style], axis=1)
df_baru = df_baru.drop(columns='Style')
df_baru
news = df_baru['Variety'].values
label = df_baru[['Bar', 'Bowl', 'Box', 'Can', 'Cup', 'Pack', 'Tray']].values
from sklearn.model_selection import train_test_split
news_train, news_test, label_train, label_test = train_test_split(news, label, test_size=0.2)
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
tokenizer = Tokenizer(num_words=5000, oov_token='zn')
tokenizer.fit_on_texts(news_train)
sekuens_latih = tokenizer.texts_to_sequences(news_train)
sekuens_test = tokenizer.texts_to_sequences(news_test)
padded_latih = pad_sequences(sekuens_latih)
padded_test = pad_sequences(sekuens_test)
import tensorflow as tf
from tensorflow.python.keras import regularizers
model = tf.keras.Sequential([
tf.keras.layers.Embedding(input_dim=5000, output_dim=16),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(128,activation = 'relu', kernel_regularizer=regularizers.l2(0.001)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(7, activation='softmax')
])
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.75 and logs.get('val_accuracy')>0.75):
print("\nAkurasi pada training set dan validation set telah mencapai >70%!")
self.model.stop_training = True
callbacks = myCallback()
num_epochs = 50
history = model.fit(padded_latih, label_train, epochs=num_epochs,
validation_data=(padded_test, label_test), verbose=2, callbacks=[callbacks])
import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Akurasi Model')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Loss Model')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()