This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pylab as plt | |
import networkx as nx | |
# mapping denote the corresponding edges between nodes | |
points_list = [(0,1), (1,5), (5,6), (5,4), (1,2), (2,3), (2,7)] | |
goal = 3 | |
G=nx.Graph() | |
G.add_edges_from(points_list) | |
pos = nx.spring_layout(G) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# assign zeros to paths and 200 to goal-reaching point | |
for point in points_list: | |
print(point) | |
if point[1] == goal: | |
R[point] = 200 | |
else: | |
R[point] = 0 | |
if point[0] == goal: | |
R[point[::-1]] = 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This equation, known as the Bellman equation, tells us that the maximum future reward. | |
Q[current_state, action] = R[current_state, action] + gamma * max_value | |
print('max_value', R[current_state, action] + gamma * max_value) | |
if (np.max(Q) > 0): | |
return(np.sum(Q/np.max(Q)*100)) | |
else: | |
return (0) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scores = [] | |
for i in range(700): | |
current_state = np.random.randint(0, int(Q.shape[0])) | |
available_act = available_actions(current_state) | |
action = sample_next_action(available_act) | |
score = update(current_state,action,gamma) | |
scores.append(score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#initial state from where the bot starts | |
current_state = 7 | |
steps = [current_state] | |
while current_state != 3: | |
next_step_index = np.where(Q[current_state,] == np.max(Q[current_state,]))[1] | |
if next_step_index.shape[0] > 1: | |
next_step_index = int(np.random.choice(next_step_index, size = 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def attention_3d_block(hidden_states): | |
""" | |
@param hidden_states: 3D tensor with shape (batch_size, time_steps, input_dim). | |
@return: 2D tensor with shape (batch_size, 128) | |
""" | |
hidden_size = int(hidden_states.shape[2]) | |
score_first_part = Dense(hidden_size, use_bias=False, name='attention_score_vec')(hidden_states) | |
h_t = Lambda(lambda x: x[:, -1, :], output_shape=(hidden_size,), name='last_hidden_state')(hidden_states) | |
score = dot([score_first_part, h_t], [2, 1], name='attention_score') | |
attention_weights = Activation('softmax', name='attention_weight')(score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#checking if all packages are installed after importing | |
print('Python: {}'.format(sys.version)) | |
print('NLTK: {}'.format(nltk.__version__)) | |
print('Scikit-learn: {}'.format(sklearn.__version__)) | |
print('Pandas: {}'.format(pd.__version__)) | |
print('Numpy: {}'.format(np.__version__)) | |
# load the dataset of SMS messages | |
df = pd.read_csv('spam.csv', header=None, encoding= "ISO-8859-1") | |
df.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import Label Encoder from scikit learn | |
from sklearn.preprocessing import LabelEncoder | |
encoder = LabelEncoder() | |
Y = encoder.fit_transform(classes) #1,0 | |
#removing not-wanted characters using regular expressions - Remove punctuation,whitespaces, | |
processed = processed.str.replace(r'[^\w\d\s]', ' ') | |
processed = processed.str.replace(r'\s+', ' ') | |
processed = processed.str.replace(r'^\s+|\s+?$', '') | |
#stemming of words | |
PS = nltk.PorterStemmer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nltk.tokenize import word_tokenize | |
# create bags of words | |
all_words = [] | |
for message in processed: | |
words = word_tokenize(message) | |
for w in words: | |
all_words.append(w) | |
#FreqDist : The FreqDist class is used to encode “frequency distributions”, which count the number of times word occurs. | |
all_words = nltk.FreqDist(all_words) | |
# use the 1500 most common words as features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# we can use sklearn algorithms in NLTK | |
from nltk.classify.scikitlearn import SklearnClassifier | |
from sklearn.neighbors import KNeighborsClassifier | |
from sklearn.tree import DecisionTreeClassifier | |
from sklearn.ensemble import RandomForestClassifier | |
from sklearn.linear_model import LogisticRegression, SGDClassifier | |
from sklearn.naive_bayes import MultinomialNB | |
from sklearn.svm import SVC | |
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix |
OlderNewer