classifier trainer based on the multilayer perceptron

Solutions on MaxInterview for classifier trainer based on the multilayer perceptron by the best coders in the world

showing results for - "classifier trainer based on the multilayer perceptron"
Jacob
12 Jan 2018
1# Classifier trainer based on the Multilayer Perceptron
2
3from pyspark.ml.linalg import Vectors
4df = spark.createDataFrame([
5  (0.0, Vectors.dense([0.0, 0.0])),
6  (1.0, Vectors.dense([0.0, 1.0])),
7  (1.0, Vectors.dense([1.0, 0.0])),
8  (0.0, Vectors.dense([1.0, 1.0]))], ["label", "features"])
9mlp = MultilayerPerceptronClassifier(maxIter=100, layers=[2, 2, 2], blockSize=1, seed=123)
10model = mlp.fit(df)
11model.layers
12# [2, 2, 2]
13model.weights.size
14# 12
15testDF = spark.createDataFrame([
16  (Vectors.dense([1.0, 0.0]),),
17  (Vectors.dense([0.0, 0.0]),)], ["features"])
18model.transform(testDF).show()
19# +---------+----------+
20# | features|prediction|
21# +---------+----------+
22# |[1.0,0.0]|       1.0|
23# |[0.0,0.0]|       0.0|
24# +---------+----------+
25# ...
26mlp_path = temp_path + "/mlp"
27mlp.save(mlp_path)
28mlp2 = MultilayerPerceptronClassifier.load(mlp_path)
29mlp2.getBlockSize()
30# 1
31model_path = temp_path + "/mlp_model"
32model.save(model_path)
33model2 = MultilayerPerceptronClassificationModel.load(model_path)
34model.layers == model2.layers
35# True
36model.weights == model2.weights
37# True
38mlp2 = mlp2.setInitialWeights(list(range(0, 12)))
39model3 = mlp2.fit(df)
40model3.weights != model2.weights
41# True
42model3.layers == model.layers
43# True