Kaggle competition: Two Sigma: Using News to Predict Stock Movements
<SOS> metric: custom Kaggle data source: two-sigma-financial-news<init_hyperparams>
pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 %matplotlib inline
def int_parameter(level, maxval):

 return int(level * maxval / 10)
 def float_parameter(level, maxval):

 return float(level)* maxval / 10.
 def sample_level(n):
return np.random.uniform(low=0.1, high=n)
 def autocontrast(pil_img, _):
return ImageOps.autocontrast(pil_img)
 def equalize(pil_img, _):
return ImageOps.equalize(pil_img)
 def posterize(pil_img, level):
level = int_parameter(sample_level(level), 4)
 return ImageOps.posterize(pil_img, 4 - level)
 def rotate(pil_img, level):
degrees = int_parameter(sample_level(level), 30)
 if np.random.uniform() > 0.5:
degrees = -degrees
return pil_img.rotate(degrees, resample=Image.BILINEAR)
 def solarize(pil_img, level):
level = int_parameter(sample_level(level), 256)
 return ImageOps.solarize(pil_img, 256 - level)
 def shear_x(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
 if np.random.uniform() > 0.5:
level = -level
return pil_img.transform(( SIZE, SIZE),
Image.AFFINE,(1, level, 0, 0, 1, 0),
resample=Image.BILINEAR)
 def shear_y(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
 if np.random.uniform() > 0.5:
level = -level
return pil_img.transform(( SIZE, SIZE),
Image.AFFINE,(1, 0, 0, level, 1, 0),
resample=Image.BILINEAR)
 def translate_x(pil_img, level):
level = int_parameter(sample_level(level), SIZE / 3)
 if np.random.random() > 0.5:
level = -level
return pil_img.transform(( SIZE, SIZE),
Image.AFFINE,(1, 0, level, 0, 1, 0),
resample=Image.BILINEAR)
 def translate_y(pil_img, level):
level = int_parameter(sample_level(level), SIZE / 3)
 if np.random.random() > 0.5:
level = -level
return pil_img.transform(( SIZE, SIZE),
Image.AFFINE,(1, 0, 0, 0, 1, level),
resample=Image.BILINEAR)
 augmentations = [
autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
translate_x, translate_y
]
MEAN = [ 0.06922848809290576, 0.06922848809290576, 0.06922848809290576]
STD = [ 0.20515700083327537, 0.20515700083327537, 0.20515700083327537]
def normalize(image):

 image = image.transpose(2, 0, 1)
 mean, std = np.array(MEAN), np.array(STD)
 image =(image - mean[:, None, None])/ std[:, None, None]
return image.transpose(1, 2, 0)
 def apply_op(image, op, severity):
image = np.clip(image * 255., 0, 255 ).astype(np.uint8)
 pil_img = Image.fromarray(image)
 pil_img = op(pil_img, severity)
 return np.asarray(pil_img)/ 255.
 def augment_and_mix(image, severity=1, width=3, depth=1, alpha=1.) :

 ws = np.float32(
 np.random.dirichlet([alpha] * width))
m = np.float32(np.random.beta(alpha, alpha))
mix = np.zeros_like(image)
 for i in range(width):
image_aug = image.copy()
depth = depth if depth > 0 else np.random.randint(1, 4)
 for _ in range(depth):
op = np.random.choice(augmentations)
 image_aug = apply_op(image_aug, op, severity)
 mix = np.add(mix, ws[i] * normalize(image_aug), out=mix,
casting="unsafe")
 mixed =(1 - m)* normalize(image)+ m * mix
return mixed<categorify>
env = twosigmanews.make_env() 
(market_train_df, news_train_df)= env.get_training_data()
class GraphemeGenerator(Sequence):
def __init__(self, data, batch_size, dim, shuffle=True, transform=None):
self._data = data
self._label_1 = pd.get_dummies(self._data['grapheme_root'],
columns = ['grapheme_root'])
 self._label_2 = pd.get_dummies(self._data['vowel_diacritic'],
columns = ['vowel_diacritic'])
 self._label_3 = pd.get_dummies(self._data['consonant_diacritic'],
columns = ['consonant_diacritic'])
 self._list_idx = data.index.values
self._batch_size = batch_size
self._dim = dim
self._shuffle = shuffle
self.transform = transform
self.on_epoch_end()
def __len__(self):
return int(np.floor(len(self._data)/self._batch_size))
def __getitem__(self, index):
batch_idx = self._indices[index*self._batch_size:(index+1)*self._batch_size]
_idx = [self._list_idx[k] for k in batch_idx]
Data = np.empty(( self._batch_size, *self._dim, 1))
Target_1 = np.empty(( self._batch_size, 168), dtype = int)
 Target_2 = np.empty(( self._batch_size, 11), dtype = int)
 Target_3 = np.empty(( self._batch_size, 7), dtype = int)
 for i, k in enumerate(_idx):
image = cv2.imread(im_path + self._data['image_id'][k] + '.png')
 image = cv2.resize(image, self._dim)
 if self.transform is not None:
if np.random.rand() > 0.7:
res = self.transform(image=image)
 image = res['image']
else:
image = augment_and_mix(image)
 image =(image.astype(np.float32)/255.0 - stats[0])/stats[1]
gray = lambda rgb : np.dot(rgb[..., :3] , [0.299 , 0.587, 0.114])
 image = gray(image)
 image = image[:, :, np.newaxis]
Data[i,:, :, :] = image
Target_1[i,:] = self._label_1.loc[k, :].values
Target_2[i,:] = self._label_2.loc[k, :].values
Target_3[i,:] = self._label_3.loc[k, :].values
return Data, [Target_1, Target_2, Target_3]
def on_epoch_end(self):
self._indices = np.arange(len(self._list_idx))
if self._shuffle:
np.random.shuffle(self._indices )<install_modules>
news_cols_agg = {
'bodySize': ['min', 'max', 'mean', 'std'],
'sentimentNegative': ['min', 'max', 'mean', 'std'],
'sentimentNeutral': ['min', 'max', 'mean', 'std'],
'sentimentPositive': ['min', 'max', 'mean', 'std'],
}
categorical_cols = ['assetName', 'dayofweek', 'month', 'year']
embedding_lengths = [100, 2, 2, 3]
encodings = {}
!pip install.. /input/efficientnet-keras-source-code/repository/qubvel-efficientnet-c993591<choose_model_class>
def join_market_news(market_train_df, news_train_df):
news_train_df['assetCodes'] = news_train_df['assetCodes'].str.findall(f"'([\w\./]+)'")
 assetCodes_expanded = list(chain(*news_train_df['assetCodes']))
assetCodes_index = news_train_df.index.repeat(news_train_df['assetCodes'].apply(len))
assert len(assetCodes_index)== len(assetCodes_expanded)
 df_assetCodes = pd.DataFrame({'level_0': assetCodes_index, 'assetCode': assetCodes_expanded})
 news_cols = ['time', 'assetCodes'] + sorted(news_cols_agg.keys())
 news_train_df_expanded = pd.merge(df_assetCodes, news_train_df[news_cols], left_on='level_0', right_index=True, suffixes=(['','_old']))
del news_train_df, df_assetCodes
news_train_df_aggregated = news_train_df_expanded.groupby(['time', 'assetCode'] ).agg(news_cols_agg)
 del news_train_df_expanded
news_train_df_aggregated = news_train_df_aggregated.apply(np.float32)
 news_train_df_aggregated.columns = ['_'.join(col ).strip() for col in news_train_df_aggregated.columns.values]
market_train_df = market_train_df.join(news_train_df_aggregated, on=['time', 'assetCode'])
 del news_train_df_aggregated
return market_train_df
wg = '.. /input/efficientnet-keras-weights-b0b5/efficientnet-b0_imagenet_1000_notop.h5'
efnet = efn.EfficientNetB0(weights=wg,
include_top = False,
input_shape=(128, 128, 3))<import_modules>
def get_xy(market_train_df, news_train_df):
x = get_x(market_train_df, news_train_df)
 y = market_train_df['returnsOpenNextMktres10'].clip(-1, 1)
 return x, y
def label_encode(series, min_counts=2):
vc = series.value_counts()
le = {c : i+1 for i, c in enumerate(vc.index[vc > min_counts])}
le['UNKN'] = 0
return le
def get_encodings(df, cat_cols):
if len(encodings)== 0:
for col in cat_cols:
encodings[col] = label_encode(df[col])
 return encodings
def map_encodings(df, cat_cols, encs):
for col in cat_cols:
df[col] = df[col].map(encs[col] ).fillna(0 ).astype(int)
 def get_x(market_train_df, news_train_df, isTrain=True):
news_train_df['time'] =(news_train_df['time'] - np.timedelta64(22,'h')).dt.ceil('1D')
 market_train_df['time'] = market_train_df['time'].dt.floor('1D')
 x = join_market_news(market_train_df, news_train_df)
 x['dayofweek'], x['day'], x['month'], x['year'] = x.time.dt.dayofweek, x.time.dt.day, x.time.dt.month, x.time.dt.year
encodings = get_encodings(x, categorical_cols)
 map_encodings(x, categorical_cols, encodings)
 if isTrain:
cols_to_drop = ['returnsOpenNextMktres10', 'universe', 'time']
else:
cols_to_drop = ['time']
x.drop(columns=cols_to_drop, inplace=True)
 return x
class GroupNormalization(Layer):

 def __init__(self,
groups=32,
axis=-1,
epsilon=1e-5,
center=True,
scale=True,
beta_initializer='zeros',
gamma_initializer='ones',
beta_regularizer=None,
gamma_regularizer=None,
beta_constraint=None,
gamma_constraint=None,
**kwargs):
super(GroupNormalization, self ).__init__(**kwargs)
 self.supports_masking = True
self.groups = groups
self.axis = axis
self.epsilon = epsilon
self.center = center
self.scale = scale
self.beta_initializer = initializers.get(beta_initializer)
 self.gamma_initializer = initializers.get(gamma_initializer)
 self.beta_regularizer = regularizers.get(beta_regularizer)
 self.gamma_regularizer = regularizers.get(gamma_regularizer)
 self.beta_constraint = constraints.get(beta_constraint)
 self.gamma_constraint = constraints.get(gamma_constraint)
 def build(self, input_shape):
dim = input_shape[self.axis]
if dim is None:
raise ValueError('Axis ' + str(self.axis)+ ' of '
'input tensor should have a defined dimension '
'but the layer received an input with shape ' +
str(input_shape)+ '.')
 if dim < self.groups:
raise ValueError('Number of groups(' + str(self.groups)+ ')cannot be '
'more than the number of channels(' +
str(dim)+ ' ).')
 if dim % self.groups != 0:
raise ValueError('Number of groups(' + str(self.groups)+ ')must be a '
'multiple of the number of channels(' +
str(dim)+ ' ).')
 self.input_spec = InputSpec(ndim=len(input_shape),
axes={self.axis: dim})
 shape =(dim,)
 if self.scale:
self.gamma = self.add_weight(shape=shape,
name='gamma',
initializer=self.gamma_initializer,
regularizer=self.gamma_regularizer,
constraint=self.gamma_constraint)
 else:
self.gamma = None
if self.center:
self.beta = self.add_weight(shape=shape,
name='beta',
initializer=self.beta_initializer,
regularizer=self.beta_regularizer,
constraint=self.beta_constraint)
 else:
self.beta = None
self.built = True
def call(self, inputs, **kwargs):
input_shape = K.int_shape(inputs)
 tensor_input_shape = K.shape(inputs)
 reduction_axes = list(range(len(input_shape)))
 del reduction_axes[self.axis]
broadcast_shape = [1] * len(input_shape)
 broadcast_shape[self.axis] = input_shape[self.axis] // self.groups
broadcast_shape.insert(1, self.groups)
 reshape_group_shape = K.shape(inputs)
 group_axes = [reshape_group_shape[i] for i in range(len(input_shape)) ]
group_axes[self.axis] = input_shape[self.axis] // self.groups
group_axes.insert(1, self.groups)
 group_shape = [group_axes[0], self.groups] + group_axes[2:]
group_shape = K.stack(group_shape)
 inputs = K.reshape(inputs, group_shape)
 group_reduction_axes = list(range(len(group_axes)))
 group_reduction_axes = group_reduction_axes[2:]
mean = K.mean(inputs, axis=group_reduction_axes, keepdims=True)
 variance = K.var(inputs, axis=group_reduction_axes, keepdims=True)
 inputs =(inputs - mean)/(K.sqrt(variance + self.epsilon))
inputs = K.reshape(inputs, group_shape)
 outputs = inputs
if self.scale:
broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
 outputs = outputs * broadcast_gamma
if self.center:
broadcast_beta = K.reshape(self.beta, broadcast_shape)
 outputs = outputs + broadcast_beta
outputs = K.reshape(outputs, tensor_input_shape)
 return outputs
def get_config(self):
config = {
'groups': self.groups,
'axis': self.axis,
'epsilon': self.epsilon,
'center': self.center,
'scale': self.scale,
'beta_initializer': initializers.serialize(self.beta_initializer),
'gamma_initializer': initializers.serialize(self.gamma_initializer),
'beta_regularizer': regularizers.serialize(self.beta_regularizer),
'gamma_regularizer': regularizers.serialize(self.gamma_regularizer),
'beta_constraint': constraints.serialize(self.beta_constraint),
'gamma_constraint': constraints.serialize(self.gamma_constraint)
 }
base_config = super(GroupNormalization, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
def compute_output_shape(self, input_shape):
return input_shap<choose_model_class>
X, y = get_xy(market_train_df, news_train_df )
for i, layer in enumerate(efnet.layers):
if "batch_normalization" in layer.name:
efnet.layers[i] = GroupNormalization(groups=32,
axis=-1,
epsilon=0.00001 )<choose_model_class>
universe = market_train_df['universe']
time = market_train_df['time']
del market_train_df, news_train_df
def E0(input_dim, output_dim, base_model):
input_tensor = Input(input_dim)
 x = Conv2D(3,(3, 3), padding='same', kernel_initializer='he_uniform',
bias_initializer='zeros' )(input_tensor)
 curr_output = base_model(x)
 curr_output = GlobalAveragePooling2D()(curr_output)
 curr_output = Dropout(0.5 )(curr_output)
 curr_output = Dense(512, activation='elu' )(curr_output)
 curr_output = Dropout(0.5 )(curr_output)
 oputput1 = Dense(168, activation='softmax', name='gra' )(curr_output)
 oputput2 = Dense(11, activation='softmax', name='vow' )(curr_output)
 oputput3 = Dense(7, activation='softmax', name='cons' )(curr_output)
 output_tensor = [oputput1, oputput2, oputput3]
model = Model(input_tensor, output_tensor)
 return model
model_E0 = E0(input_dim=(128,128,1),
output_dim=(168,11,7), base_model = efnet )<choose_model_class>
num_cols = [x for x in X.columns if x not in categorical_cols]
num_cols = [x for x in num_cols if x not in ['assetCode']]
def D169(input_dim, output_dim, base_model):
input_tensor = Input(input_dim)
 x = Conv2D(3,(3, 3), padding='same', kernel_initializer='he_uniform',
bias_initializer='zeros' )(input_tensor)
 curr_output = base_model(x)
 curr_output = BatchNormalization()(curr_output)
 curr_output = GlobalAveragePooling2D()(curr_output)
 curr_output = Dense(512, activation='relu' )(curr_output)
 curr_output = Dropout(0.5 )(curr_output)
 curr_output = Dense(1024, activation='relu' )(curr_output)
 oputput1 = Dense(168, activation='softmax', name='gra' )(curr_output)
 oputput2 = Dense(11, activation='softmax', name='vow' )(curr_output)
 oputput3 = Dense(7, activation='softmax', name='cons' )(curr_output)
 output_tensor = [oputput1, oputput2, oputput3]
model = Model(input_tensor, output_tensor)
 return model
densenet = DenseNet169(include_top=False, weights=None,
input_shape=(128, 128, 3))
model_D169 = D169(input_dim=(128,128,1),
output_dim=(168,11,7), base_model = densenet )<init_hyperparams>
def scale_numeric(df):
df[num_cols] = df[num_cols].fillna(0)
 scaler = StandardScaler()
for i in range(0, len(num_cols), 4):
cols = num_cols[i:i + 3]
df[cols] = scaler.fit_transform(df[cols].astype(float))
scale_numeric(X )
class AdaBound(keras.optimizers.Optimizer):

 def __init__(self, learning_rate=0.001, final_lr=0.1, beta_1=0.9, beta_2=0.999, gamma=1e-3,
epsilon=None, decay=0., amsbound=False, weight_decay=0.0, **kwargs):
super(AdaBound, self ).__init__(**kwargs)
 if not 0.<= gamma <= 1.:
raise ValueError("Invalid `gamma` parameter.Must lie in [0, 1] range.")
 with K.name_scope(self.__class__.__name__):
self.iterations = K.variable(0, dtype='int64', name='iterations')
 self.learning_rate = K.variable(learning_rate, name='learning_rate')
 self.beta_1 = K.variable(beta_1, name='beta_1')
 self.beta_2 = K.variable(beta_2, name='beta_2')
 self.decay = K.variable(decay, name='decay')
 self.final_lr = final_lr
self.gamma = gamma
if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.initial_decay = decay
self.amsbound = amsbound
self.weight_decay = float(weight_decay)
 self.base_lr = float(learning_rate)
 def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
 self.updates = [K.update_add(self.iterations, 1)]
learning_rate = self.learning_rate
if self.initial_decay > 0:
learning_rate = learning_rate *(1./(1.+ self.decay * K.cast(self.iterations,
K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx())+ 1
step_size = learning_rate *(K.sqrt(1.- K.pow(self.beta_2, t)) / 
(1.- K.pow(self.beta_1, t)))
 final_lr = self.final_lr * learning_rate / self.base_lr
lower_bound = final_lr *(1.- 1./(self.gamma * t + 1.))
 upper_bound = final_lr *(1.+ 1./(self.gamma * t))
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
if self.amsbound:
vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
else:
vhats = [K.zeros(1)for _ in params]
self.weights = [self.iterations] + ms + vs + vhats
for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
if self.weight_decay != 0.:
g += self.weight_decay * K.stop_gradient(p)
 m_t =(self.beta_1 * m)+(1.- self.beta_1)* g
v_t =(self.beta_2 * v)+(1.- self.beta_2)* K.square(g)
 if self.amsbound:
vhat_t = K.maximum(vhat, v_t)
 denom =(K.sqrt(vhat_t)+ self.epsilon)
 self.updates.append(K.update(vhat, vhat_t))
else:
denom =(K.sqrt(v_t)+ self.epsilon)
 step_size_p = step_size * K.ones_like(denom)
 step_size_p_bound = step_size_p / denom
bounded_lr_t = m_t * K.minimum(K.maximum(step_size_p_bound,
lower_bound), upper_bound)
 p_t = p - bounded_lr_t
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
new_p = p_t
if getattr(p, 'constraint', None)is not None:
new_p = p.constraint(new_p)
 self.updates.append(K.update(p, new_p))
return self.updates
def get_config(self):
config = {'learning_rate': float(K.get_value(self.learning_rate)) ,
'final_lr': float(self.final_lr),
'beta_1': float(K.get_value(self.beta_1)) ,
'beta_2': float(K.get_value(self.beta_2)) ,
'gamma': float(self.gamma),
'decay': float(K.get_value(self.decay)) ,
'epsilon': self.epsilon,
'weight_decay': self.weight_decay,
'amsbound': self.amsbound}
base_config = super(AdaBound, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))<choose_model_class>
n_train = int(X.shape[0] * 0.8)
 X_train, y_train = X.iloc[:n_train], y.iloc[:n_train]
X_valid, y_valid = X.iloc[n_train:], y.iloc[n_train:]
for model in [model_E0, model_D169]:
model.compile(
 optimizer = AdaBound(learning_rate=0.001,
final_lr=.1,
gamma=1e-3,
weight_decay=5e-4,
amsbound=False),
loss = {'gra' : 'categorical_crossentropy',
'vow' : 'categorical_crossentropy',
'cons': 'categorical_crossentropy'},
loss_weights = {'gra' : 1.0,
'vow' : 1.0,
'cons': 1.0},
metrics={'gra' : 'accuracy',
'vow' : 'accuracy',
'cons': 'accuracy'} 
)
<train_model>
u_valid =(universe.iloc[n_train:] > 0)
 t_valid = time.iloc[n_train:]
X_valid = X_valid[u_valid]
y_valid = y_valid[u_valid]
t_valid = t_valid[u_valid]
d_valid = t_valid.dt.date
del u_valid
plot_model(model_E0, to_file='model.png' )<define_variables>
def get_cat_num_split(df):
X = {}
X['num'] = df.loc[:, num_cols].values
X['num'] = np.reshape(X['num'],(X['num'].shape[0], 1, X['num'].shape[1]))
for cat in categorical_cols:
X[cat] = df.loc[:, cat].values
return X
plot_model(model_D169, to_file='model.png' )<split>
X_train_split = get_cat_num_split(X_train)
 X_valid_split = get_cat_num_split(X_valid)
 y_train_bin =(y_train >= 0 ).values
y_valid_bin =(y_valid >= 0 ).values
transforms_train = albumentations.Compose([
GridMask(num_grid=3, rotate=15, p=1),
])
 train_labels, val_labels = train_test_split(train, test_size = 0.10, random_state = SEED,
stratify = train[['grapheme_root',
'vowel_diacritic',
'consonant_diacritic']])
 train_generator = GraphemeGenerator(train_labels, batch_size, dim,
shuffle = True, transform=transforms_train)
 val_generator = GraphemeGenerator(val_labels, batch_size, dim,
shuffle = False )<choose_model_class>
encoding_len = {k: len(encodings[k])+ 1 for k in encodings}
EarlyStopping, ReduceLROnPlateau, TensorBoard, CSVLogger)
 def Call_Back() :
checkpoint = ModelCheckpoint('E0Train_B0.h5',
monitor = 'val_loss',
verbose = 0, save_best_only=True,
mode = 'min',
save_weights_only = True)
 csv_logger = CSVLogger('E0.csv')
 early = EarlyStopping(monitor='val_loss',
mode='min', patience=5)
 return [checkpoint, csv_logger, early]
epochs = 60
training = False
callbacks = Call_Back()
if training:
train_history = model.fit_generator(
 train_generator,
steps_per_epoch=int(len(train_labels)/batch_size),
validation_data=val_generator,
validation_steps = int(len(val_labels)/batch_size),
epochs=epochs,
callbacks=callbacks 
)
 else:
model_E0.load_weights('.. /input/0909e0/0.909.h5')
 model_D169.load_weights('.. /input/newdensenet/newde69.h5' )<define_variables>
DROPOUT_RATE = 0.2
cat_inputs = [Input(shape=[1], name=cat)for cat in categorical_cols]
embeddings = [Embedding(encoding_len[cat], embedding_lengths[i] )(cat_inputs[i])for i, cat in enumerate(categorical_cols)]
categorical_logits = Concatenate()([(cat_emb)for cat_emb in embeddings])
 categorical_logits = LSTM(128, activation='relu', input_shape=(1, len(categorical_cols)) , return_sequences=True,
kernel_regularizer=l2(1e-5), kernel_initializer='random_uniform' )(categorical_logits)
 numerical_inputs = Input(shape=(1, len(num_cols)) , name='num')
 numerical_logits = LSTM(256, activation='relu', input_shape=(1, len(num_cols)) , return_sequences=True,
kernel_regularizer=l2(1e-5), kernel_initializer='random_uniform' )(numerical_inputs)
 numerical_logits = Dropout(DROPOUT_RATE )(numerical_logits)
 logits = Concatenate()([numerical_logits,categorical_logits])
 logits = LSTM(256, activation='relu', kernel_initializer='random_uniform' )(logits)
 out = Dense(1, activation='sigmoid', name='confidence_level' )(logits)
 model = Model(inputs = cat_inputs + [numerical_inputs], outputs=out)
 model.compile(loss='binary_crossentropy', optimizer='adam' )
def test_batch_generator(df, batch_size):
num_imgs = len(df)
 for batch_start in range(0, num_imgs, batch_size):
curr_batch_size = min(num_imgs, batch_start + batch_size)- batch_start
idx = np.arange(batch_start, batch_start + curr_batch_size)
 names_batch = df.iloc[idx, 0].values
imgs_batch = 255 - df.iloc[idx, 1:].values.reshape(-1, HEIGHT, WIDTH ).astype(np.uint8)
 X_batch = np.zeros(( curr_batch_size, SIZE, SIZE, 1))
for j in range(curr_batch_size):
img =(imgs_batch[j,]*(255.0/imgs_batch[j,].max())).astype(np.uint8)
 img = crop_resize(img, size=SIZE)
 img =(img.astype(np.float32)/255.0 - stats[0])/stats[1]
img = img[:, :, np.newaxis]
X_batch[j,] = img
yield X_batch, names_batch
TEST = [
".. /input/bengaliai-cv19/test_image_data_0.parquet",
".. /input/bengaliai-cv19/test_image_data_1.parquet",
".. /input/bengaliai-cv19/test_image_data_2.parquet",
".. /input/bengaliai-cv19/test_image_data_3.parquet",
]
row_id = []
target = []
for fname in tqdm(TEST):
test_ = pd.read_parquet(fname)
 test_gen = test_batch_generator(test_, batch_size=batch_size)
 for batch_x, batch_name in test_gen:
batch_predict_1 = model_E0.predict(batch_x, batch_size = 128)
 batch_predict_2 = model_D169.predict(batch_x, batch_size = 128)
 for idx, name in enumerate(batch_name):
row_id += [
f"{name}_consonant_diacritic",
f"{name}_grapheme_root",
f"{name}_vowel_diacritic",
]
target += [
np.argmax(( batch_predict_1[2] + batch_predict_2[2])/2, axis=1)[idx],
np.argmax(( batch_predict_1[0] + batch_predict_2[0])/2, axis=1)[idx],
np.argmax(( batch_predict_1[1] + batch_predict_2[1])/2, axis=1)[idx],
]
del test_
gc.collect()
df_sample = pd.DataFrame(
 {
'row_id': row_id,
'target':target
},
columns = ['row_id','target'] 
)
 df_sample.to_csv('submission.csv',index=False)
 gc.collect()<import_modules>
check_point = ModelCheckpoint('model.hdf5',verbose=True, save_best_only=True)
 early_stop = EarlyStopping(patience=1,verbose=True)
 model.fit(X_train_split, y_train_bin,
validation_data=(X_valid_split, y_valid_bin),
epochs=1,
verbose=True,
callbacks=[early_stop,check_point] )
import torch
import numpy as np
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
from torchvision import transforms as torchtransforms
import cv2
import torch.nn as nn
from tqdm import tqdm_notebook as tqdm
import torchvision
import torch.nn as nn
import torch.nn.functional as F
import time<define_variables>
x_t_i = confidence_valid * y_valid
data = {'day' : d_valid, 'x_t_i' : x_t_i}
df = pd.DataFrame(data)
 x_t = df.groupby('day' ).sum().values.flatten()
mean = np.mean(x_t)
 std = np.std(x_t)
 score_valid = mean / std
print(score_valid )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device)
 modelpath = "/kaggle/input/se-resnext50-32x4d-fold2/se_resnext50_32x4d_fold2.pkl"
root_path="/kaggle/input/bengaliai-cv19"<categorify>
def make_predictions(predictions_template_df, market_obs_df, news_obs_df):
inp = get_x(market_obs_df, news_obs_df, False)
 scale_numeric(inp)
 inp_split = get_cat_num_split(inp)
 scaled_pred = model.predict(inp_split)* 2 - 1
predictions_template_df.confidenceValue = np.clip(scaled_pred, -1, 1 )
simple_transform_valid = torchtransforms.Compose([
torchtransforms.ToTensor() ,
torchtransforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ] )<choose_model_class>
days = env.get_prediction_days()
x1,y1,z1 = None, None, None
for(market_obs_df, news_obs_df, predictions_template_df)in days:
x1,y1,z1 = predictions_template_df, market_obs_df, news_obs_df
make_predictions(x1,y1,z1)
 env.predict(predictions_template_df)
 print('Done!' )
<load_pretrained><EOS> 
env.write_submission_file()