Skip to content

Commit 8e0fb9d

Browse files
author
l2k2
committed
amazon project
1 parent 63c3d08 commit 8e0fb9d

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

amazon-reviews/amazon-bow.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import amazon
2+
import numpy as np
3+
from keras.models import Sequential
4+
from keras.layers import Dense, Dropout, Activation
5+
from keras.layers import Embedding, LSTM
6+
from keras.layers import Conv1D, Flatten
7+
from keras.preprocessing import text
8+
import wandb
9+
from wandb.keras import WandbCallback
10+
11+
wandb.init()
12+
config = wandb.config
13+
config.vocab_size = 1000
14+
15+
(train_summary, train_review_text, train_labels), (test_summary, test_review_text, test_labels) = amazon.load_amazon()
16+
17+
tokenizer = text.Tokenizer(num_words=config.vocab_size)
18+
tokenizer.fit_on_texts(train_review_text)
19+
X_train = tokenizer.texts_to_matrix(train_review_text)
20+
X_test = tokenizer.texts_to_matrix(test_review_text)
21+
22+
# Build the model
23+
model = Sequential()
24+
model.add(Dense(1, activation='softmax', input_shape=(config.vocab_size,)))
25+
26+
model.compile(loss='binary_crossentropy',
27+
optimizer='adam',
28+
metrics=['accuracy'])
29+
model.fit(X_train, train_labels, epochs=10, validation_data=(X_test, test_labels),
30+
callbacks=[WandbCallback()])

amazon-reviews/amazon.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import json
3+
4+
def load_amazon():
5+
filename = 'reviews_Video_Games_5.json'
6+
train_summary = []
7+
train_review_text = []
8+
train_labels = []
9+
10+
test_summary = []
11+
test_review_text = []
12+
test_labels = []
13+
14+
with open(filename, 'r') as f:
15+
for (i, line) in enumerate(f):
16+
data = json.loads(line)
17+
18+
if data['overall'] == 3:
19+
next
20+
elif data['overall'] == 4 or data['overall'] == 5:
21+
label = 1
22+
elif data['overall'] == 1 or data['overall'] == 2:
23+
label = 0
24+
else:
25+
raise Exception("Unexpected value " + str(data['overall']))
26+
27+
summary = data['summary']
28+
review_text = data['reviewText']
29+
30+
if (i % 10 == 0):
31+
test_summary.append(summary)
32+
test_review_text.append(review_text)
33+
test_labels.append(label)
34+
else:
35+
train_summary.append(summary)
36+
train_review_text.append(review_text)
37+
train_labels.append(label)
38+
39+
return (train_summary, train_review_text, train_labels), (test_summary, test_review_text, test_labels)
40+
41+
load_amazon()

amazon-reviews/download-amazon.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
wget http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/reviews_Video_Games_5.json.gz
2+
gunzip xvfz reviews_Video_Games_5.json.gz

amazon-reviews/wandb/settings

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[default]
2+
entity: qualcomm
3+
project: amazon-sep13
4+
base_url: https://api.wandb.ai

0 commit comments

Comments
 (0)