def test_train_custom_builder2(self): """ Build a custom Regression model whose Model graph has the rhombic design """ print("Test 1: Rhombic Design\n") # Define the builder builder = StaticBuilder('CustBuild') enc_dirs = { 'numlayers': 2, 'numnodes': 128, 'activations': 'leaky_relu', 'netgrowrate': 1.0 } input_dim = 2 in0 = builder.addInput(input_dim, name="Features") enc1 = builder.addTransformInner(10, main_inputs=in0, directives=enc_dirs) enc2 = builder.addTransformInner(10, main_inputs=in0, directives=enc_dirs) builder.addTransformInner(1, main_inputs=[enc1, enc2], numlayers=1, activations='linear', name='Prediction') reg = Regression(input_dim=2, output_dim=1, builder=builder) # save_on_valid_improvement=True) # OK! reg.train(dataset, num_epochs=10)
def test_train_custom_builder(self): """ """ print("Test 0: Chain of Encoders\n") # Define a builder. Get more control on the directives of each node enc_dirs = { 'numlayers': 2, 'numnodes': 128, 'activations': 'leaky_relu', 'netgrowrate': 1.0 } input_dim, output_dim = 2, 1 builder = StaticBuilder('CustBuild') in0 = builder.addInput(input_dim, name="Features") enc1 = builder.addTransformInner(5, main_inputs=in0, **enc_dirs) builder.addTransformInner(state_size=1, main_inputs=enc1, directives=enc_dirs, name='Prediction') # Pass the builder object to the Regression Model reg = Regression(builder=builder, output_dim=output_dim) # save_on_valid_improvement=True) # ok! reg.train(dataset, num_epochs=10)
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(self): """ Test train """ print("Test 0: Regression train") dc = Regression(input_dim=2, output_dim=1, save_on_valid_improvement=False) # ok! dc.train(dataset, num_epochs=20) # train
def test_train_custom_builder(self): """ """ print("Running Test 0\n") x = 10.0 * np.random.randn(100, 2) 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} # Define a builder. # More control on the directives of each node builder = StaticBuilder() enc_dirs = { 'num_layers': 2, 'num_nodes': 128, 'activation': 'leaky_relu', 'net_grow_rate': 1.0 } input_dim, output_dim = 2, 1 in0 = builder.addInput(input_dim, name="features") enc1 = builder.addInner(1, 10, directives=enc_dirs) enc2 = builder.addInner(1, output_dim, directives=enc_dirs) out0 = builder.addOutput(name="prediction") builder.addDirectedLink(in0, enc1) builder.addDirectedLink(enc1, enc2) builder.addDirectedLink(enc2, out0) # Pass the builder object to the Regression Model reg = Regression(builder=builder) reg.build() reg.visualize_model_graph("two_encoders.png") reg.train(dataset, num_epochs=50)
def test_init(self): """ Test initialization """ print("\nTest 0: Regression initialization") Regression(input_dim=10, output_dim=1)
def test_train_custom_builder2(self): """ Build a custom Regression model whose Model graph has the rhombic design """ print("Running Test 1\n") x = 10.0 * np.random.randn(100, 2) 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} # Define a builder, control the graph # Like a lot more control... builder = StaticBuilder() enc_dirs = { 'num_layers': 2, 'num_nodes': 128, 'activation': 'leaky_relu', 'net_grow_rate': 1.0 } input_dim = 2 in0 = builder.addInput(input_dim, name="features") enc1 = builder.addInner(10, num_islots=1, directives=enc_dirs) enc2 = builder.addInner(10, num_islots=1, directives=enc_dirs) enc3 = builder.addInner(1, num_islots=2, num_layers=1, activation='linear') out0 = builder.addOutput(name="prediction") builder.addDirectedLink(in0, enc1) builder.addDirectedLink(in0, enc2) builder.addDirectedLink(enc1, enc3, islot=0) builder.addDirectedLink(enc2, enc3, islot=1) builder.addDirectedLink(enc3, 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)
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)
def test_train(self): """ Test train """ x = 10.0 * np.random.randn(100, 2) y = x[:, 0:1] + 1.5 * x[:, 1:] # + 3*x[:,1:]**2 + 0.5*np.random.randn(100,1) xtrain, xvalid, ytrain, yvalid = x[:80], x[80:], y[:80], y[80:] train_dataset = {'train_features': xtrain, 'train_response': ytrain} dc = Regression(input_dim=2, output_dim=1) dc.build() dc.train(train_dataset, num_epochs=50) dataset = {'features': xvalid} Ypred = dc.sample(dataset, islot=0) print("Ypred", list(zip(Ypred.tolist(), yvalid.tolist())))
def test_build(self): """ Test build """ model = Regression(input_dim=10, output_dim=1) model.build()