public static void Run() { var input = new Input(new Keras.Shape(32, 32)); //var a = new CuDNNLSTM(32).Set(input); var a = new Dense(32, activation: "sigmoid").Set(input); //a.Set(input); var output = new Dense(1, activation: "sigmoid").Set(a); //output.Set(a); var model = new Keras.Models.Model(new Input[] { input }, new BaseLayer[] { output }); //Load train data Numpy.NDarray x = np.array(new float[, ] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }); NDarray y = np.array(new float[] { 0, 1, 1, 0 }); var input1 = new Input(new Shape(32, 32, 3)); var conv1 = new Conv2D(32, (4, 4).ToTuple(), activation: "relu").Set(input1); var pool1 = new MaxPooling2D((2, 2).ToTuple()).Set(conv1); var flatten1 = new Flatten().Set(pool1); var input2 = new Input(new Shape(32, 32, 3)); var conv2 = new Conv2D(16, (8, 8).ToTuple(), activation: "relu").Set(input2); var pool2 = new MaxPooling2D((2, 2).ToTuple()).Set(conv2); var flatten2 = new Flatten().Set(pool2); var merge = new Concatenate(flatten1, flatten2); }
public static void Run() { //Load train data NDarray x = np.array(new float[, ] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }); NDarray y = np.array(new float[] { 0, 1, 1, 0 }); //Build functional model var input = new Input(shape: new Keras.Shape(2)); var hidden1 = new Dense(32, activation: "relu").Set(input); var hidden2 = new Dense(64, activation: "relu").Set(hidden1); var output = new Dense(1, activation: "sigmoid").Set(hidden2); var model = new Keras.Models.Model(new Input[] { input }, new BaseLayer[] { output }); //Compile and train model.Compile(optimizer: new Adam(), loss: "binary_crossentropy", metrics: new string[] { "accuracy" }); var history = model.Fit(x, y, batch_size: 2, epochs: 10, verbose: 1); //var weights = model.GetWeights(); //model.SetWeights(weights); var logs = history.HistoryLogs; //Save model and weights string json = model.ToJson(); File.WriteAllText("model.json", json); model.SaveWeight("model.h5"); //Load model and weight var loaded_model = Sequential.ModelFromJson(File.ReadAllText("model.json")); loaded_model.LoadWeight("model.h5"); }