The partial that appeared in The code of the 1st place in the Mercari competition I didn't really understand.
partial → Some arguments are variable, some arguments are fixed and processing can be executed.
Reference of the example below Fix b at 5, and change the argument of a from 0 to 5 for processing.
qiita.rb
from functools import partial
def add_func(a, b):
return a + b
add_list = list(map(partial(add_func, b=5), [0,1,2,3,4,5]))
print(add_list)
Example in Mercari Competition 1st Code The value of y_train is fixed, and only the value of xs that goes into fit_predict is changed.
qiita.rb
def fit_predict(xs, y_train) -> np.ndarray:
X_train, X_test = xs
config = tf.ConfigProto(
intra_op_parallelism_threads=1, use_per_session_threads=1, inter_op_parallelism_threads=1)
with tf.Session(graph=tf.Graph(), config=config) as sess, timer('fit_predict'):
ks.backend.set_session(sess)
model_in = ks.Input(shape=(X_train.shape[1],), dtype='float32', sparse=True)
out = ks.layers.Dense(192, activation='relu')(model_in)
out = ks.layers.Dense(64, activation='relu')(out)
out = ks.layers.Dense(64, activation='relu')(out)
out = ks.layers.Dense(1)(out)
model = ks.Model(model_in, out)
model.compile(loss='mean_squared_error', optimizer=ks.optimizers.Adam(lr=3e-3))
for i in range(3):
with timer(f'epoch {i + 1}'):
model.fit(x=X_train, y=y_train, batch_size=2**(11 + i), epochs=1, verbose=0)
return model.predict(X_test)[:, 0]
with ThreadPool(processes=4) as pool: #4 threads
Xb_train, Xb_valid = [x.astype(np.bool).astype(np.float32) for x in [X_train, X_valid]]
xs = [[Xb_train, Xb_valid], [X_train, X_valid]] * 2
y_pred = np.mean(pool.map(partial(fit_predict, y_train=y_train), xs), axis=0)
Recommended Posts