System.Collections.Generic.KeyNotFoundException: The given key '1' was not present in the dictionary.
wasd52030 opened this issue · 1 comments
Description
Hi, I'm currently trying to run a f# program to train a simple CNN module
and here comes a problem :
Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key '1' was not present in the dictionary.
at System.Collections.Generic.Dictionary2.get_Item(TKey key) at Tensorflow.Keras.Engine.Node.MapArguments(Dictionary2 tensor_dict)
at Tensorflow.Keras.Engine.Functional.Call(Tensors inputs, Tensor state, Nullable1 training) at Tensorflow.Keras.Engine.Layer.Apply(Tensors inputs, Tensor state, Boolean training) at Tensorflow.Keras.Engine.Model.train_step(DataHandler data_handler, Tensors x, Tensors y) at Tensorflow.Keras.Engine.Model.train_step_function(DataHandler data_handler, OwnedIterator iterator) at Tensorflow.Keras.Engine.Model.FitInternal(DataHandler data_handler, Int32 epochs, Int32 verbose, List1 callbackList, Nullable1 validation_data, Func3 train_step_func)
at Tensorflow.Keras.Engine.Model.fit(NDArray x, NDArray y, Int32 batch_size, Int32 epochs, Int32 verbose, List1 callbacks, Single validation_split, Nullable1 validation_data, Boolean shuffle, Int32 initial_epoch, Int32 max_queue_size, Int32 workers, Boolean use_multiprocessing)
at Program.train(IModel model, NDArray x_train, NDArray y_train) in C:\Users\user\Downloads\fsCNN\Program.fs:line 40
at <StartupCode$fsCNN>.$Program.main@() in C:\Users\user\Downloads\fsCNN\Program.fs:line 47
and here is my code and environment
open type Tensorflow.KerasApi
open Tensorflow
open Tensorflow.NumPy
open type Tensorflow.Keras.Utils.np_utils
let layers = keras.layers
let input =
keras.Input(Shape(28, 28, 1))
|> layers.Conv2D(16, Shape(5, 5), activation = "relu").Apply
|> layers.MaxPooling2D(Shape(2, 2)).Apply
|> layers.Conv2D(36, Shape(5, 5), activation = "relu").Apply
|> layers.MaxPooling2D(Shape(2, 2)).Apply
|> layers.Flatten().Apply
|> layers.Dense(128, activation = "relu").Apply
let output = input |> layers.Dense(10, activation = "relu").Apply
let model = keras.Model(input, output, "CNN")
let prepareData () =
let (x_train, y_train, x_test, y_test) =
keras.datasets.mnist.load_data().Deconstruct()
let x_train = (x_train.reshape (Shape(-1, 28, 28, 1))).astype (TF_DataType.TF_FLOAT)
let x_test = (x_test.reshape (Shape(-1, 28, 28, 1))).astype (TF_DataType.TF_FLOAT)
let x_train = (x_train / 255).numpy ()
let x_test = (x_test / 255).numpy ()
let y_train = to_categorical (y_train, 10)
let y_test = to_categorical (y_test, 10)
(x_train, y_train, x_test, y_test)
let train (model: Tensorflow.Keras.Engine.IModel) (x_train: NDArray, y_train) =
model.compile (keras.optimizers.Adam(), keras.losses.CategoricalCrossentropy(), metrics = [| "acc" |])
model.fit (x_train, y_train, validation_split = 0.2f, batch_size = 200, epochs = 20, verbose = 1)
|> ignore
model.save ("fsCNN")
let (x_train, y_train, x_test, y_test) = prepareData ()
train model (x_train, y_train)
Windows 10 22H2(19045.4921)
dotnet sdk 8.0.204
NumSharp 0.30.0
SciSharp.TensorFlow.Redist-Windows-GPU 2.10.3
TensorFlow.Keras 0.10.5
TensorFlow.NET 0.100.5
and similar logic works in C#...
I don't know how to fix this problem.
I'd appreciate it if you could give me any suggestions or comments.
Thank you
Have a nice day!
ok, I solve the error
when build model, it should be write below
let cnn =
let input = keras.Input(Shape(28, 28, 1))
let modelFlow =
input
|> layers.Conv2D(16, Shape(5, 5), activation = "relu").Apply
|> layers.MaxPooling2D(Shape(2,2)).Apply
|> layers.Conv2D(36, Shape(5, 5), activation = "relu").Apply
|> layers.MaxPooling2D(Shape(2,2)).Apply
|> layers.Flatten().Apply
|> layers.Dense(128, activation = "relu").Apply
let output = modelFlow |> layers.Dense(10, activation = "softmax").Apply
keras.Model(input, output, "CNN")