def test_init(self): """ Create a CustomNode """ tf.reset_default_graph() builder = StaticBuilder() builder.createCustomNode(1, 1, name="Custom")
def test_CallCustom(self): """ Test, calling a CustomNode """ print("\nTest 1: Build") builder = StaticBuilder("MyModel") cust = builder.createCustomNode(num_inputs=1, num_outputs=1, name="Custom") cust_in1 = cust.addInner(3) cust_in2 = cust.addInner(4) cust.addDirectedLink(cust_in1, cust_in2, islot=0) cust.declareIslot(islot=0, innernode_name=cust_in1, inode_islot=0) cust.declareOslot(oslot='main', innernode_name=cust_in2, inode_oslot='main') in1 = builder.addInput(10) builder.addDirectedLink(in1, cust, islot=0) builder.build() X = tf.placeholder(tf.float32, [1, 10], 'X') Y = cust(X) # pylint: disable=unpacking-non-sequence print('Y', Y) self.assertEqual(Y[0].shape[-1], 4, "")
def test_train_custom_node2(self): """ Test a custom Regression model with a CustomNode """ print("Test 2: with CustomNode\n") builder = StaticBuilder("MyModel") enc_dirs = { 'numlayers': 2, 'numnodes': 128, 'activations': 'leaky_relu', 'netgrowrate': 1.0 } input_dim = 2 in0 = builder.addInput(input_dim, name='Features') cust = builder.createCustomNode(inputs=[in0], num_outputs=1, name="Prediction") cust_in1 = cust.addTransformInner(3, main_inputs=[0], directives=enc_dirs) cust_in2 = cust.addTransformInner(1, main_inputs=cust_in1, directives=enc_dirs) cust.declareOslot(oslot=0, innernode_name=cust_in2, inode_oslot_name='main') reg = Regression(builder=builder, input_dim=2, output_dim=1) # save_on_valid_improvement=True) # ok! reg.train(dataset, num_epochs=10)
def test_train_custom_node2(self): """ Test commit """ print("Test 1: with CustomNode\n") builder = StaticBuilder("MyModel") enc_dirs = { 'loc_numlayers': 2, 'loc_numnodes': 64, 'loc_activations': 'softplus', 'loc_netgrowrate': 1.0 } input_dim = 10 in0 = builder.addInput(input_dim, name='Observation') # Define Custom Recognition Model cust_rec = builder.createCustomNode(inputs=[in0], num_outputs=1, name="Recognition") cust_in2 = cust_rec.addTransformInner(3, main_inputs=[0], node_class=NormalTriLNode, **enc_dirs) cust_rec.declareOslot(oslot=0, innernode_name=cust_in2, inode_oslot_name='main') # Define Custom Generative Model cust_gen = builder.createCustomNode(inputs=cust_rec.name, num_outputs=1, name="Generative") cust_ginn1 = cust_gen.addTransformInner(16, main_inputs=[0]) cust_ginn2 = cust_gen.addTransformInner(10, main_inputs=cust_ginn1, node_class=NormalTriLNode, **enc_dirs) cust_gen.declareOslot(oslot=0, innernode_name=cust_ginn2, inode_oslot_name='main') # Define VAE and train vae = VariationalAutoEncoder(builder=builder) vae.train(dataset, num_epochs=10)
def test_init(self): """ Create a CustomNode """ print("Test 0: Initialization") builder = StaticBuilder(scope='BuildCust') i1 = builder.addInput([[3]], name='In1') cust = builder.createCustomNode(i1, 3, name="Custom") print("cust.name", cust.name) print("cust.num_expected_inputs", cust.num_expected_inputs) print("cust.num_expected_outputs", cust.num_expected_outputs)
def test_add_encoder0(self): """ Test commit """ print("Test 1: Adding nodes to Custom") builder = StaticBuilder("MyModel") i1 = builder.addInput(10) cust = builder.createCustomNode(inputs=i1, num_outputs=1, name="Custom") cust.addTransformInner(3, main_inputs=[0]) print("cust.in_builder.inode_to_innernode", cust.in_builder.islot_to_innernode_names)
def test_add_encoder0(self): """ Test commit """ tf.reset_default_graph() builder = StaticBuilder("MyModel") builder.addInput(10) cust = builder.createCustomNode(1, 1, name="Custom") cust_in1 = cust.addInner(3) cust_in2 = cust.addInner(4) cust.addDirectedLink(cust_in1, cust_in2) cust.commit() builder.addOutput()
def test_add_encoder1(self): """ Test commit """ print("Test 2: Adding more nodes to Custom") builder = StaticBuilder("MyModel") i1 = builder.addInput(10) cust = builder.createCustomNode(inputs=i1, num_outputs=1, name="Custom") ctin1 = cust.addTransformInner(3, [0]) cust.addTransformInner(4, [ctin1]) print("cust.in_builder._inode_to_innernode", cust.in_builder.islot_to_innernode_names) print("cust.in_builder.input_nodes", cust.in_builder.input_nodes)
def test_build0(self): """ Test commit """ print("Test 4: Basic Custom build") builder = StaticBuilder("MyModel") i1 = builder.addInput(10) cust = builder.createCustomNode(inputs=i1, num_outputs=1, name="Custom") ctin = cust.addTransformInner(3, main_inputs=[0]) cust.declareOslot(0, ctin, 'main') builder.build()
def test_add_encoder2(self): """ Test commit """ print("Test 3: Declaring multiple inputs to the Custom Node") builder = StaticBuilder("MyModel") i1 = builder.addInput(10) i2 = builder.addInput(5) cust = builder.createCustomNode(inputs=[i1,i2], num_outputs=1, name="Custom") ctin1 = cust.addTransformInner(3, [0]) cust.addTransformInner(3, [1, ctin1]) print("cust.in_builder._inode_to_innernode", cust.in_builder.islot_to_innernode_names) print("cust.in_builder.input_nodes", cust.in_builder.input_nodes)
def test_build3(self): """ Test build """ print("Test 7: Slightly more complicated Custom build") builder = StaticBuilder("MyModel") i1 = builder.addInput(10) i2 = builder.addInput(5) cust = builder.createCustomNode(inputs=[i1,i2], num_outputs=1, name="Custom") ctin1 = cust.addTransformInner(3, [0]) ctin2 = cust.addTransformInner(3, [1, ctin1]) cust.declareOslot(oslot=0, innernode_name=ctin2, inode_oslot_name='main') builder.build()
def test_build_outputs(self): """ Test commit """ print("Test 8: Building outputs") builder = StaticBuilder("MyModel") i1 = builder.addInput(10) cust = builder.createCustomNode(inputs=i1, num_outputs=1, name="Custom") ctin = cust.addTransformInner(3, main_inputs=[0]) cust.declareOslot(0, ctin, 'main') builder.build() X = tf.placeholder(tf.float64, [None, 10]) cust.build_outputs(imain0=X)
def test_build_outputs1(self): """ Test build """ print("Test 9: Building outputs") builder = StaticBuilder("MyModel") i1 = builder.addInput(10) i2 = builder.addInput(5) cust = builder.createCustomNode(inputs=[i1,i2], num_outputs=1, name="Custom") ctin1 = cust.addTransformInner(3, [0]) ctin2 = cust.addTransformInner(3, [0, ctin1, 1]) cust.declareOslot(oslot=0, innernode_name=ctin2, inode_oslot_name='main') builder.build() X = tf.placeholder(tf.float64, [None, 10]) Y = tf.placeholder(tf.float64, [None, 5]) cust.build_outputs(imain0=X, imain1=Y)
def test_train_custom_node2(self): """ Test commit """ print("Test 2: with CustomNode\n") input_dim = 2 x = 10.0 * np.random.randn(100, input_dim) y = x[:, 0:1] + 1.5 * x[:, 1:] # + 3*x[:,1:]**2 + 0.5*np.random.randn(100,1) dataset = {'train_features': x, 'train_input_response': y} builder = StaticBuilder("MyModel") enc_dirs = { 'num_layers': 2, 'num_nodes': 128, 'activation': 'leaky_relu', 'net_grow_rate': 1.0 } in0 = builder.addInput(input_dim, name='features') cust = builder.createCustomNode(1, 1, name="Custom") cust_in1 = cust.addInner(3, directives=enc_dirs) cust_in2 = cust.addInner(1, directives=enc_dirs) cust.addDirectedLink(cust_in1, cust_in2) cust.addInput(islot=0, inode_name=cust_in1, inode_islot=0) cust.addOutput(oslot=0, inode_name=cust_in2, inode_oslot=0) cust.commit() out0 = builder.addOutput(name='prediction') builder.addDirectedLink(in0, cust) builder.addDirectedLink(cust, out0) # Pass the builder object to the Regression Model reg = Regression(input_dim=2, output_dim=1, builder=builder) reg.build() reg.visualize_model_graph("two_encoders.png") reg.train(dataset, num_epochs=50)