layer.innerProduct.weights.floatValue.extend(weights) layer.innerProduct.bias.floatValue.extend(biases) spec.neuralNetworkClassifier.stringClassLabels.vector.extend(labels) # Make this model trainable. builder = NeuralNetworkBuilder(spec=model._spec) builder.make_updatable(["fullyconnected0"]) builder.set_categorical_cross_entropy_loss(name="lossLayer", input="labelProbability") builder.set_epochs(10, [1, 10, 50]) # Using the SDG optimizer: sgd_params = SgdParams(lr=0.001, batch=8, momentum=0) sgd_params.set_batch(8, [1, 2, 8, 16]) builder.set_sgd_optimizer(sgd_params) # Using the Adam optimizer: # adam_params = AdamParams(lr=0.001, batch=8, beta1=0.9, beta2=0.999, eps=1e-8) # adam_params.set_batch(8, [1, 2, 8, 16]) # builder.set_adam_optimizer(adam_params) builder.spec.description.trainingInput[0].shortDescription = "Example image" builder.spec.description.trainingInput[1].shortDescription = "True label" coremltools.utils.save_spec(builder.spec, "../Models/HandsTuri.mlmodel") # Replace the weights of the last layer with random weights. model = coremltools.models.MLModel("../Models/HandsTuri.mlmodel")
input_features = [('data', datatypes.Array(*input_dim))] output_features = [('result', datatypes.Array(*output_dim))] weights = np.random.rand(1, input_max_size) bias = np.random.rand(1) builder = NeuralNetworkBuilder(input_features, output_features) builder.add_inner_product(name='ip_layer', W=weights, b=bias, input_channels=input_max_size, output_channels=1, has_bias=True, input_name='data', output_name='result') builder.make_updatable(['ip_layer']) builder.set_mean_squared_error_loss(name='lossLayer', input_feature=output_features[0]) optimizerParams = SgdParams(lr=0.01, batch=1) optimizerParams.set_batch(1, allowed_set=[1, 2, 4, 8, 16, 32]) builder.set_sgd_optimizer(optimizerParams) builder.set_epochs(16, allowed_set=[2, 4, 8, 16, 32, 64, 128, 256]) #builder.spec = convert_neural_network_spec_weights_to_fp16(builder.spec) save_spec(builder.spec, '../core/LinearRegressionModel.mlmodel')