Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
input_node1 = ak.Input()
temp_node1 = ak.Normalization()(input_node1)
output_node1 = ak.DenseBlock()(temp_node1)
output_node3 = ak.Normalization()(temp_node1)
output_node3 = ak.DenseBlock()(output_node3)
input_node2 = ak.Input()
output_node2 = ak.Normalization()(input_node2)
output_node2 = ak.DenseBlock()(output_node2)
output_node = ak.Merge()([output_node1, output_node2, output_node3])
output_node = ak.RegressionHead()(output_node)
graph = ak.GraphAutoModel([input_node1, input_node2],
output_node,
directory=tmp_dir,
max_trials=1)
graph.fit([x_train, x_train], y_train,
epochs=1,
batch_size=100,
validation_data=([x_train, x_train], y_train),
validation_split=0.5,
verbose=False)
def test_structured_data_input(tmp_dir):
num_data = 500
data = common.structured_data(num_data)
x_train = data
y = np.random.randint(0, 3, num_data)
y_train = y
input_node = ak.StructuredDataInput(
column_names=common.COLUMN_NAMES_FROM_NUMPY,
column_types=common.COLUMN_TYPES_FROM_NUMPY)
output_node = input_node
output_node = ak.StructuredDataBlock()(output_node)
output_node = ak.ClassificationHead(loss='categorical_crossentropy',
metrics=['accuracy'])(output_node)
auto_model = ak.GraphAutoModel(input_node,
output_node,
directory=tmp_dir,
max_trials=1)
auto_model.fit(x_train, y_train, epochs=1,
validation_data=(x_train, y_train))
auto_model.predict(x_train)
def test_resnet_block(_, tmp_dir):
x_train = np.random.rand(100, 32, 32, 3)
y_train = np.random.randint(10, size=100)
y_train = tf.keras.utils.to_categorical(y_train)
input_node = ak.Input()
output_node = input_node
output_node = ak.ResNetBlock()(output_node)
output_node = ak.ClassificationHead()(output_node)
graph = ak.GraphAutoModel(input_node, output_node,
directory=tmp_dir,
max_trials=1)
graph.fit(x_train, y_train,
epochs=1,
batch_size=100,
verbose=False,
validation_split=0.2)
def test_rnn_block(tmp_dir):
x_train = np.random.rand(100, 32, 10)
y_train = np.random.randint(5, size=100)
y_train = tf.keras.utils.to_categorical(y_train)
input_node = ak.Input()
output_node = input_node
output_node = ak.RNNBlock()(output_node)
output_node = ak.ClassificationHead()(output_node)
input_node.shape = (32, 10)
output_node[0].shape = (5,)
graph = ak.GraphAutoModel(input_node, output_node,
directory=tmp_dir,
max_trials=1)
graph.fit(x_train, y_train,
epochs=1,
batch_size=100,
verbose=False,
validation_split=0.2)
result = graph.predict(x_train)
assert result.shape == (100, 5)
def test_merge(tmp_dir):
x_train = np.random.rand(100, 33)
y_train = np.random.rand(100, 1)
input_node1 = ak.Input()
input_node2 = ak.Input()
output_node1 = ak.DenseBlock()(input_node1)
output_node2 = ak.DenseBlock()(input_node2)
output_node = ak.Merge()([output_node1, output_node2])
output_node = ak.RegressionHead()(output_node)
graph = ak.GraphAutoModel([input_node1, input_node2],
output_node,
directory=tmp_dir,
max_trials=1)
graph.fit([x_train, x_train], y_train,
epochs=1,
batch_size=100,
verbose=False,
validation_split=0.5)
result = graph.predict([x_train, x_train])
assert result.shape == (100, 1)
def test_input_output_disconnect(tmp_dir):
input_node1 = ak.Input()
output_node = input_node1
_ = ak.DenseBlock()(output_node)
input_node = ak.Input()
output_node = input_node
output_node = ak.DenseBlock()(output_node)
output_node = ak.RegressionHead()(output_node)
with pytest.raises(ValueError) as info:
ak.GraphAutoModel(input_node1, output_node, directory=tmp_dir)
assert str(info.value) == 'Inputs and outputs not connected.'
def test_hyper_graph_cycle(tmp_dir):
input_node1 = ak.Input()
input_node2 = ak.Input()
output_node1 = ak.DenseBlock()(input_node1)
output_node2 = ak.DenseBlock()(input_node2)
output_node = ak.Merge()([output_node1, output_node2])
head = ak.RegressionHead()
output_node = head(output_node)
head.outputs = output_node1
with pytest.raises(ValueError) as info:
ak.GraphAutoModel([input_node1, input_node2],
output_node,
directory=tmp_dir)
assert str(info.value) == 'The network has a cycle.'
def test_input_missing(tmp_dir):
input_node1 = ak.Input()
input_node2 = ak.Input()
output_node1 = ak.DenseBlock()(input_node1)
output_node2 = ak.DenseBlock()(input_node2)
output_node = ak.Merge()([output_node1, output_node2])
output_node = ak.RegressionHead()(output_node)
with pytest.raises(ValueError) as info:
ak.GraphAutoModel(input_node1, output_node, directory=tmp_dir)
assert str(info.value).startswith('A required input is missing for HyperModel')
y_regression = np.random.rand(x_train.shape[0], 1)
y_classification = y_classification.reshape(-1, 1)
# Build model and train.
inputs = ak.ImageInput(shape=(28, 28, 1))
outputs1 = ak.ResNetBlock(version='next')(inputs)
outputs2 = ak.XceptionBlock()(inputs)
image_outputs = ak.Merge()((outputs1, outputs2))
structured_inputs = ak.StructuredDataInput()
structured_outputs = ak.DenseBlock()(structured_inputs)
merged_outputs = ak.Merge()((structured_outputs, image_outputs))
classification_outputs = ak.ClassificationHead()(merged_outputs)
regression_outputs = ak.RegressionHead()(merged_outputs)
automodel = ak.GraphAutoModel(inputs=[inputs, structured_inputs],
outputs=[regression_outputs,
classification_outputs])
automodel.fit((x_image, x_structured),
(y_regression, y_classification),
# trials=100,
validation_split=0.2,
epochs=200,
callbacks=[tf.keras.callbacks.EarlyStopping()])