<jupyter_start><jupyter_text>Load data<jupyter_code># train_data_path = conf.dataset_path + 'part-210204-2'
train_data_path = conf.raw_lzo_path + 'part-00000'
train_df = read_data(train_data_path)
train_df.head()
# val_data_path = conf.dataset_path + 'part-210205-2'
val_data_path = conf.raw_lzo_path + 'part-00001'
val_df = read_data(val_data_path)
train_df.columns<jupyter_output><empty_output><jupyter_text>Parameters<jupyter_code>MODEL_NAME = 'autoencoder_test'
BATCH_SIZE = 2
MAX_SEQUENCE = 25
EPOCH = 30
UNITS = 1024
EMBEDDING_DIM = 256
VALIDATION_SPLIT = 0.1<jupyter_output><empty_output><jupyter_text>Preprocessing  dataset preprocess<jupyter_code>ds = Dataset(train=True, target_encoding=False)
train_df = ds.preprocess(train_df)
val_df = ds.preprocess(val_df)<jupyter_output><empty_output><jupyter_text>negative samping<jupyter_code>train_df
train_df_positive = train_df[train_df['like']==1]
train_df_negative = train_df[train_df['like']==0]
print(len(train_df_positive))
print(len(train_df_negative))

# train_df_positive = train_df_positive.sample(n = len(train_df_negative))
train_df_negative = train_df_negative.sample(n = len(train_df_positive))
train_df = pd.concat([train_df_positive, train_df_negative])
train_df.head()

train_df.columns
conf.target
used_features = ['engager_follower_count', 
                'engager_following_count', 
                'engager_is_verified', 
                'engager_account_creation', 
                'creator_follower_count', 
                'creator_following_count', 
                'creator_is_verified', 
                'creator_account_creation',
                'media',
                'domains',
                'language',
                'dt_day', 
                'dt_dow', 
                'dt_hour', 
                'len_domains']
X_train = train_df[used_features]
# Y_train = train_df['like_timestamp'].fillna(0).apply(lambda x: 1 if x > 0 else 0)
Y_train = train_df['like']
X_val = val_df[used_features]
# Y_val = val_df['like_timestamp'].fillna(0).apply(lambda x: 1 if x > 0 else 0) 
Y_val = val_df['like']
X_val, X_test, Y_val, Y_test = train_test_split(X_val, Y_val, test_size=0.5)<jupyter_output><empty_output><jupyter_text>scaling<jupyter_code>def scaling(df_x):
    standard_scaler = preprocessing.StandardScaler()
    standard_scaler.fit(df_x.values)
    ss = standard_scaler.transform(df_x.values)
    df_x = pd.DataFrame(ss, columns = df_x.columns)
    return df_x
X_train = scaling(X_train)
X_val = scaling(X_val)
X_test = scaling(X_test)
X_train<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>kargs = {
    'model_name': 'auto_encoder',
    'input_dim' : X_train.shape[1],
    # 'input_dim': 100000,
    'engager_dim' : 4,
    'creator_dim' : 4,
    'embedding_dim': 4,
    'encoder_dim': 64,
    'decoder_dim': 1
    
}

class AutoEncoder(tf.keras.models.Model):
    def __init__(self, **kargs):
        super(AutoEncoder, self).__init__()
        
        self.input_dim = kargs['input_dim']
        self.engager_dim = kargs['engager_dim']
        self.creator_dim = kargs['creator_dim']
        self.embedding_dim = kargs['embedding_dim']
        self.encoder_dim = kargs['encoder_dim']
        self.decoder_dim = kargs['decoder_dim']
        
        self.engager_embedding = tf.keras.layers.Embedding(input_dim=self.engager_dim, output_dim=self.embedding_dim)
        self.creator_embedding = tf.keras.layers.Embedding(input_dim=self.creator_dim, output_dim=self.embedding_dim)
        self.engager_encoder = tf.keras.layers.Dense(16, activation = 'relu')
        self.creator_encoder = tf.keras.layers.Dense(16, activation = 'relu')
        self.tweet_encoder = tf.keras.layers.Dense(16, activation = 'relu')
        self.encoder = tf.keras.layers.Dense(8, activation = 'relu')
        # self.encoder2 = tf.keras.layers.Dense(4, activation = 'relu')
        self.decoder = tf.keras.layers.Dense(1, activation = 'relu')
    
    def call(self, x):
        # x1 = self.engager_embedding(x[:,:4])
        # x2 = self.creator_embedding(x[:,4:])
        x1 = self.engager_encoder(x[:,:4])
        x2 = self.creator_encoder(x[:,4:8])


        x3 = self.tweet_encoder(x[:,8:])
        x = tf.concat([x1, x2, x3], 1)
        x = self.encoder(x)
        # x = tf.concat([x, x3], 1)
        # x = self.encoder2(x)
        x = self.decoder(x)
        return x

    def summary(self):
        inputs = tf.keras.layers.Input((self.input_dim))
        tf.keras.models.Model(inputs, self.call(inputs)).summary()
    
model = AutoEncoder(**kargs)

model.summary()
# opt = tf.keras.optimizers.Adam(learning_rate=0.01)
model.compile(
    optimizer = 'adam', # adam
    loss = 'binary_crossentropy', # softmax : sparse_categorical_crossentropy, sigmoid : binary_crossentropy
    metrics=['binary_crossentropy'])
result = model.fit(
    x = X_train.to_numpy(),
    y = Y_train.to_numpy(),
    validation_data=(X_val.to_numpy(), Y_val.to_numpy()),
    epochs=10,
    batch_size=32
)<jupyter_output>Epoch 1/10
 4476/94810 [>.............................] - ETA: 33s - loss: 0.8580 - binary_crossentropy: 0.8580<jupyter_text>Evaluate<jupyter_code>plt.plot(result.history['binary_crossentropy'])
plt.plot(result.history['val_binary_crossentropy'])
plt.title('model accuracy')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

pred = model.predict(X_test)
pred.reshape(-1)
rce = compute_rce(pred.astype(np.float64), Y_test.astype(np.float64))
rce
average_precision_score(Y_test, pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13956004327443203, 'fraction_numerical': 0.02289938694554634, 'mean_word_length': 3.8151041666666665, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '277416', 'license': 'Apache-2.0', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2045, 'n_words': 283, 'path': 'models/notebooks/Autoencoder/autoencoder_embed.ipynb', 'repo_name': 'RecKIE7/recsys2021-twitter', 'size': '5546'}