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
|
def model(X, Y, word_to_vec_map, learning_rate = 0.01, num_iterations = 400): """ Model to train word vector representations in numpy.
Arguments: X -- input data, numpy array of sentences as strings, of shape (m, 1) Y -- labels, numpy array of integers between 0 and 7, numpy-array of shape (m, 1) word_to_vec_map -- dictionary mapping every word in a vocabulary into its 50-dimensional vector representation learning_rate -- learning_rate for the stochastic gradient descent algorithm num_iterations -- number of iterations
Returns: pred -- vector of predictions, numpy-array of shape (m, 1) W -- weight matrix of the softmax layer, of shape (n_y, n_h) b -- bias of the softmax layer, of shape (n_y,) """
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)
z = np.dot(W, avg) + b a = softmax(z)
cost = -1 * np.sum(Y_oh[i] * np.log(a))
dz = a - Y_oh[i] dW = np.dot(dz.reshape(n_y,1), avg.reshape(1, n_h)) db = dz
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
|
近期评论