<jupyter_start><jupyter_text>LSTM<jupyter_code>lstm = Sequential()
lstm.add(Embedding(5000, 256, input_length=X.shape[1]))
lstm.add(SpatialDropout1D(0.4))
lstm.add(LSTM(256, return_sequences=True, dropout=0.3, recurrent_dropout=0.2))
lstm.add(LSTM(256, dropout=0.3, recurrent_dropout=0.2))
lstm.add(Dense(2, activation='softmax'))
lstm.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
lstm.summary()
batch_size=32
lstm.fit(X_train, y_train, epochs = 30,batch_size=batch_size, verbose = 'auto')
lstm.evaluate(X_test,y_test)
gru = Sequential()
gru.add(Embedding(5000, 256, input_length=X.shape[1]))
gru.add(Dropout(0.3))
gru.add(GRU(256, return_sequences=True, dropout=0.3, recurrent_dropout=0.2))
gru.add(GRU(256, dropout=0.3, recurrent_dropout=0.2))
gru.add(Dense(2, activation='softmax'))
gru.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
gru.summary()
batch_size=32
gru.fit(X_train, y_train, epochs = 30,batch_size=batch_size, verbose = 'auto')
gru.evaluate(X_test,y_test)
predictions = gru.predict(X_test)
avg_neg = np.mean([prediction[0] for prediction in predictions])
avg_pos = np.mean([prediction[1] for prediction in predictions])
print(f"Average negative sentiment score = {avg_neg}\nAverage positive sentiment score = {avg_pos}")
text = "tata Consultancy Earnings Trail Estimates After Labor Crunch Boosts Costs "
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(text)
X1 = tokenizer.texts_to_sequences(text)
X1 = pad_sequences(X1)
predictions = gru.predict(X1)
avg_neg = np.mean([prediction[0] for prediction in predictions])
avg_pos = np.mean([prediction[1] for prediction in predictions])
print(f"Average negative sentiment score = {avg_neg}\nAverage positive sentiment score = {avg_pos}")
gru.save('gru.h5')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup