吴恩达深度学习course5 week2:Emojify

# -*- coding:utf-8 -*- 
#Author: shenying
#Date: 18-7-17 下午4:35

import numpy as np
from emo_utils import *
import emoji
import matplotlib.pyplot as plt

X_train,Y_train=read_csv('data2-2/train_emoji.csv')
X_test,Y_test=read_csv('data2-2/tesss.csv')
maxLen=len(max(X_train,key=len).split())
# print(Y_train)
# index=0
# print(X_train[index],label_to_emoji(Y_train[index]))
Y_oh_train=convert_to_one_hot(Y_train,C=5)
Y_oh_test=convert_to_one_hot(Y_test,C=5)
# index=50
# print(Y_train[index],'is converted into one hot',Y_oh_train[index])
word_to_index,index_to_word,word_to_vec_map=read_glove_vecs('data2/glove.6B.50d.txt')
# word = "cucumber"
# index = 289846
# print("the index of", word, "in the vocabulary is", word_to_index[word])
# print("the", str(index) + "th word in the vocabulary is", index_to_word[index])
def sentence_to_avg(sentence,word_to_vec_map):
    words=(sentence.lower()).split()
    avg=np.zeros((50,))
    for w in words:
        avg+=word_to_vec_map[w]
    avg=avg/len(words)
    return avg
# avg = sentence_to_avg("Morrocan couscous is my favorite dish", word_to_vec_map)
# print("avg = ", avg)
def model(X,Y,word_to_vec_map,learning_rate=0.001,num_iterations=2000):
    np.random.seed(1)
    m=Y.shape[0]
    n_y=5
    n_h=50

    W=np.random.randn(n_y,n_h)/np.sqrt(n_h)
    b=np.zeros((n_y,))

    Y_oh=convert_to_one_hot(Y,C=n_y)

    for t in range(num_iterations):
        for i in range(m):
            avg=sentence_to_avg(X[i],word_to_vec_map)
            # Forward propagate the avg through the softmax layer
            z = np.dot(W,avg)+b
            a = softmax(z)

            # Compute cost using the j'th training label's one hot representation and "A" (the output of the softmax)
            cost = -np.sum((Y_oh[i]*np.log(a)))
            ### END CODE HERE ###

            # Compute gradients
            dz = a - Y_oh[i]
            dW = np.dot(dz.reshape(n_y, 1), avg.reshape(1, n_h))
            db = dz

            # Update parameters with Stochastic Gradient Descent
            W = W - learning_rate * dW
            b = b - learning_rate * db

        if t % 100 == 0:
            print("Epoch: " + str(t) + " --- cost = " + str(cost))
            pred = predict(X, Y, W, b, word_to_vec_map)

    return pred, W, b

# print(X_train.shape)
# print(Y_train.shape)
# print(np.eye(5)[Y_train.reshape(-1)].shape)
# print(X_train[0])
# print(type(X_train))
# Y = np.asarray([5,0,0,5, 4, 4, 4, 6, 6, 4, 1, 1, 5, 6, 6, 3, 6, 3, 4, 4])
# print(Y.shape)
# X = np.asarray(['I am going to the bar tonight', 'I love you', 'miss you my dear',
#  'Lets go party and drinks','Congrats on the new job','Congratulations',
#  'I am so happy for you', 'Why are you feeling bad', 'What is wrong with you',
#  'You totally deserve this prize', 'Let us go play football',
#  'Are you down for football this afternoon', 'Work hard play harder',
#  'It is suprising how people can be dumb sometimes',
#  'I am very disappointed','It is the best day in my life',
#  'I think I will end up alone','My life is so boring','Good job',
#  'Great so awesome'])
pred, W, b = model(X_train, Y_train, word_to_vec_map)
# print(pred)
print("Training set:")
pred_train = predict(X_train, Y_train, W, b, word_to_vec_map)
print('Test set:')
pred_test = predict(X_test, Y_test, W, b, word_to_vec_map)
X_my_sentences = np.array(["i adore you", "i not love you", "funny lol", "lets play with a ball", "food is ready", "you are not happy"])
Y_my_labels = np.array([[0], [0], [2], [1], [4],[3]])

pred = predict(X_my_sentences, Y_my_labels , W, b, word_to_vec_map)
print_predictions(X_my_sentences, pred)

print(Y_test.shape)
print('           '+ label_to_emoji(0)+ '    ' + label_to_emoji(1) + '    ' +  label_to_emoji(2)+ '    ' + label_to_emoji(3)+'   ' + label_to_emoji(4))
print(pd.crosstab(Y_test, pred_test.reshape(56,), rownames=['Actual'], colnames=['Predicted'], margins=True))
plot_confusion_matrix(Y_test, pred_test)

enojify_v2:

# -*- coding:utf-8 -*- 
#Author: shenying
#Date: 18-7-17 下午7:37
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import numpy as np
np.random.seed(0)
from keras.models import Model
from keras.layers import Dense,Input,Dropout,LSTM,Activation
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras.models import load_model
from emo_utils import *
np.random.seed(1)

from keras.initializers import glorot_uniform
X_train,Y_train=read_csv('data2-2/train_emoji.csv')
X_test,Y_test=read_csv('data2-2/tesss.csv')
maxLen=len(max(X_train,key=len).split())
print(maxLen)
word_to_index,index_to_word,word_to_vec_map=read_glove_vecs('data2/glove.6B.50d.txt')

def sentences_to_indices(X,word_to_index,max_len):
    m=X.shape[0]
    X_indices=np.zeros((m,max_len))

    for i in range(m):
        sentence_words=(X[i]).lower().split()
        j=0
        for w in sentence_words:
            X_indices[i,j]=word_to_index[w]
            j=j+1
    return X_indices
# X1 = np.array(["funny lol", "lets play baseball", "food is ready for you"])
# X1_indices = sentences_to_indices(X1,word_to_index, max_len = 5)
# print("X1 =", X1)
# print("X1_indices =", X1_indices)
def pretrained_embedding_layer(word_to_vec_map,word_to_index):
    vocab_len = len(word_to_index) + 1  # adding 1 to fit Keras embedding (requirement)
    emb_dim = word_to_vec_map["cucumber"].shape[0]
    emb_matrix = np.zeros((vocab_len,emb_dim))
    for word, index in word_to_index.items():
        emb_matrix[index, :] = word_to_vec_map[word]
    embedding_layer = Embedding(vocab_len,emb_dim,trainable=False)
    embedding_layer.build((None,))
    embedding_layer.set_weights([emb_matrix])
    return embedding_layer
# embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index)
# print("weights[0][1][3] =", embedding_layer.get_weights()[0][1][3])
def Emojify_V2(input_shape,word_to_vec_map,word_to_index):
    sentence_indices = Input(shape=input_shape,dtype=np.int32)
    embedding_layer = pretrained_embedding_layer(word_to_vec_map,word_to_index)
    embeddings = embedding_layer(sentence_indices)
    X = LSTM(128,return_sequences=True)(embeddings)
    X = Dropout(0.5)(X)
    X = LSTM(128,return_sequences=False)(X)
    X = Dropout(0.5)(X)
    X = Dense(5,activation='softmax')(X)
    X = Activation('softmax')(X)
    model = Model(sentence_indices,X)
    return model
# model = Emojify_V2((10,), word_to_vec_map, word_to_index)
# model.summary()
# model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# X_train_indices=sentences_to_indices(X_train,word_to_index,maxLen)
# Y_train_oh=convert_to_one_hot(Y_train,C=5)
# model.fit(X_train_indices,Y_train_oh,batch_size=32,epochs=50,shuffle=True)
# model.save('emojify.h5')
# del model

# loss_train,acc_train=model.evaluate(X_train_indices,Y_train_oh)
# print('acc= ',acc_train)
#
# X_test_indices=sentences_to_indices(X_test,word_to_index,maxLen)
# Y_test_oh=convert_to_one_hot(Y_test,C=5)
# loss,acc=model.evaluate(X_test_indices,Y_test_oh)
# print()
# print('Test accuracy=',acc)

# C = 5
# y_test_oh = np.eye(C)[Y_test.reshape(-1)]
# X_test_indices = sentences_to_indices(X_test, word_to_index, maxLen)
# pred = model.predict(X_test_indices)
# for i in range(len(X_test)):
#     x = X_test_indices
#     num = np.argmax(pred[i])
#     if(num != Y_test[i]):
#         print('Expected emoji:'+ label_to_emoji(Y_test[i]) + ' prediction: '+ X_test[i] + label_to_emoji(num).strip())
model=load_model('emojify.h5')
x_test = np.array(['so cute of you'])
X_test_indices = sentences_to_indices(x_test, word_to_index, maxLen)
print(x_test[0] +' '+  label_to_emoji(np.argmax(model.predict(X_test_indices))))

猜你喜欢

转载自blog.csdn.net/qq_31119155/article/details/81162892