Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def one_hot_encoder_deserializer_test(self):
labels = ['a', 'b', 'c']
le = LabelEncoder(input_features=['label_feature'],
output_features='label_feature_le_encoded')
oh_data = le.fit_transform(labels).reshape(3, 1)
one_hot_encoder_tf = OneHotEncoder(sparse=False)
one_hot_encoder_tf.mlinit(prior_tf = le,
output_features='{}_one_hot_encoded'.format(le.output_features))
one_hot_encoder_tf.fit(oh_data)
one_hot_encoder_tf.serialize_to_bundle(self.tmp_dir, one_hot_encoder_tf.name)
# Deserialize the OneHotEncoder
node_name = "{}.node".format(one_hot_encoder_tf.name)
one_hot_encoder_tf_ds = OneHotEncoder()
one_hot_encoder_tf_ds.deserialize_from_bundle(self.tmp_dir, node_name)
# Transform some sample data
res_a = one_hot_encoder_tf.transform(oh_data)
res_b = one_hot_encoder_tf_ds.transform(oh_data)
self.assertEqual(res_a[0][0], res_b[0][0])
le = LabelEncoder(input_features=['label_feature'],
output_features='label_feature_le_encoded')
oh_data = le.fit_transform(labels).reshape(3, 1)
one_hot_encoder_tf = OneHotEncoder(sparse=False)
one_hot_encoder_tf.mlinit(prior_tf = le,
output_features='{}_one_hot_encoded'.format(le.output_features))
one_hot_encoder_tf.fit(oh_data)
one_hot_encoder_tf.serialize_to_bundle(self.tmp_dir, one_hot_encoder_tf.name)
# Deserialize the OneHotEncoder
node_name = "{}.node".format(one_hot_encoder_tf.name)
one_hot_encoder_tf_ds = OneHotEncoder()
one_hot_encoder_tf_ds.deserialize_from_bundle(self.tmp_dir, node_name)
# Transform some sample data
res_a = one_hot_encoder_tf.transform(oh_data)
res_b = one_hot_encoder_tf_ds.transform(oh_data)
self.assertEqual(res_a[0][0], res_b[0][0])
self.assertEqual(res_a[1][0], res_b[1][0])
self.assertEqual(res_a[2][0], res_b[2][0])
# Test node.json
with open("{}/{}.node/node.json".format(self.tmp_dir, one_hot_encoder_tf.name)) as json_data:
node = json.load(json_data)
self.assertEqual(one_hot_encoder_tf_ds.name, node['name'])
self.assertEqual(one_hot_encoder_tf_ds.input_features[0], node['shape']['inputs'][0]['name'])
def one_hot_encoder_serializer_test(self):
labels = ['a', 'b', 'c']
le = LabelEncoder(input_features=['label_feature'],
output_features='label_feature_le_encoded')
oh_data = le.fit_transform(labels).reshape(3, 1)
one_hot_encoder_tf = OneHotEncoder(sparse=False)
one_hot_encoder_tf.mlinit(prior_tf=le,
output_features='{}_one_hot_encoded'.format(le.output_features))
one_hot_encoder_tf.fit(oh_data)
one_hot_encoder_tf.serialize_to_bundle(self.tmp_dir, one_hot_encoder_tf.name)
# Test model.json
with open("{}/{}.node/model.json".format(self.tmp_dir, one_hot_encoder_tf.name)) as json_data:
model = json.load(json_data)
self.assertEqual(one_hot_encoder_tf.op, model['op'])
self.assertEqual(3, model['attributes']['size']['long'])
self.assertEqual(True, model['attributes']['drop_last']['boolean'])