From 0ee157f9bbae4c6beb571e5686def72b591f9119 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 20 Mar 2020 16:50:17 +0530 Subject: [PATCH] fixed error TypeError: initialize_weights() got an unexpected keyword argument 'dtype' --- Siamese on Omniglot Dataset.ipynb | 92 ++++++------------------------- 1 file changed, 17 insertions(+), 75 deletions(-) diff --git a/Siamese on Omniglot Dataset.ipynb b/Siamese on Omniglot Dataset.ipynb index 5268a34..c2b3836 100644 --- a/Siamese on Omniglot Dataset.ipynb +++ b/Siamese on Omniglot Dataset.ipynb @@ -2,19 +2,9 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 13, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", - " from ._conv import register_converters as _register_converters\n", - "Using TensorFlow backend.\n" - ] - } - ], + "outputs": [], "source": [ "import sys\n", "import numpy as np\n", @@ -29,6 +19,7 @@ "import time\n", "\n", "import tensorflow as tf\n", + "from keras.initializers import RandomNormal\n", "from keras.models import Sequential\n", "from keras.optimizers import Adam\n", "from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate\n", @@ -282,35 +273,7 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "def initialize_weights(shape, name=None):\n", - " \"\"\"\n", - " The paper, http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf\n", - " suggests to initialize CNN layer weights with mean as 0.0 and standard deviation of 0.01\n", - " \"\"\"\n", - " return np.random.normal(loc = 0.0, scale = 1e-2, size = shape)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "def initialize_bias(shape, name=None):\n", - " \"\"\"\n", - " The paper, http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf\n", - " suggests to initialize CNN layer bias with mean as 0.5 and standard deviation of 0.01\n", - " \"\"\"\n", - " return np.random.normal(loc = 0.5, scale = 1e-2, size = shape)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -326,21 +289,20 @@ " # Convolutional Neural Network\n", " model = Sequential()\n", " model.add(Conv2D(64, (10,10), activation='relu', input_shape=input_shape,\n", - " kernel_initializer=initialize_weights, kernel_regularizer=l2(2e-4)))\n", + " kernel_initializer= RandomNormal(mean=0.0, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n", " model.add(MaxPooling2D())\n", " model.add(Conv2D(128, (7,7), activation='relu',\n", - " kernel_initializer=initialize_weights,\n", - " bias_initializer=initialize_bias, kernel_regularizer=l2(2e-4)))\n", + " kernel_initializer=RandomNormal(mean=0.0, stddev=0.01, seed=1),\n", + " bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n", " model.add(MaxPooling2D())\n", - " model.add(Conv2D(128, (4,4), activation='relu', kernel_initializer=initialize_weights,\n", - " bias_initializer=initialize_bias, kernel_regularizer=l2(2e-4)))\n", + " model.add(Conv2D(128, (4,4), activation='relu', kernel_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=1) ,bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n", " model.add(MaxPooling2D())\n", - " model.add(Conv2D(256, (4,4), activation='relu', kernel_initializer=initialize_weights,\n", - " bias_initializer=initialize_bias, kernel_regularizer=l2(2e-4)))\n", + " model.add(Conv2D(256, (4,4), activation='relu', kernel_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=1) ,bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n", " model.add(Flatten())\n", " model.add(Dense(4096, activation='sigmoid',\n", " kernel_regularizer=l2(1e-3),\n", - " kernel_initializer=initialize_weights,bias_initializer=initialize_bias))\n", + " kernel_initializer=RandomNormal(mean=0.0, stddev=0.01, seed=1),\n", + " bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1)))\n", " \n", " # Generate the encodings (feature vectors) for the two images\n", " encoded_l = model(left_input)\n", @@ -351,7 +313,7 @@ " L1_distance = L1_layer([encoded_l, encoded_r])\n", " \n", " # Add a dense layer with a sigmoid unit to generate the similarity score\n", - " prediction = Dense(1,activation='sigmoid',bias_initializer=initialize_bias)(L1_distance)\n", + " prediction = Dense(1,activation='sigmoid',bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1))(L1_distance)\n", " \n", " # Connect the inputs with the outputs\n", " siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)\n", @@ -362,33 +324,13 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 16, "metadata": {}, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "__________________________________________________________________________________________________\n", - "Layer (type) Output Shape Param # Connected to \n", - "==================================================================================================\n", - "input_1 (InputLayer) (None, 105, 105, 1) 0 \n", - "__________________________________________________________________________________________________\n", - "input_2 (InputLayer) (None, 105, 105, 1) 0 \n", - "__________________________________________________________________________________________________\n", - "sequential_1 (Sequential) (None, 4096) 38947648 input_1[0][0] \n", - " input_2[0][0] \n", - "__________________________________________________________________________________________________\n", - "lambda_1 (Lambda) (None, 4096) 0 sequential_1[1][0] \n", - " sequential_1[2][0] \n", - "__________________________________________________________________________________________________\n", - "dense_2 (Dense) (None, 1) 4097 lambda_1[0][0] \n", - "==================================================================================================\n", - "Total params: 38,951,745\n", - "Trainable params: 38,951,745\n", - "Non-trainable params: 0\n", - "__________________________________________________________________________________________________\n" - ] + "name": "stdout", + "text": "WARNING:tensorflow:From C:\\Users\\Hemanth\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n\nModel: \"model_1\"\n__________________________________________________________________________________________________\nLayer (type) Output Shape Param # Connected to \n==================================================================================================\ninput_7 (InputLayer) (None, 105, 105, 1) 0 \n__________________________________________________________________________________________________\ninput_8 (InputLayer) (None, 105, 105, 1) 0 \n__________________________________________________________________________________________________\nsequential_4 (Sequential) (None, 4096) 38947648 input_7[0][0] \n input_8[0][0] \n__________________________________________________________________________________________________\nlambda_1 (Lambda) (None, 4096) 0 sequential_4[1][0] \n sequential_4[2][0] \n__________________________________________________________________________________________________\ndense_2 (Dense) (None, 1) 4097 lambda_1[0][0] \n==================================================================================================\nTotal params: 38,951,745\nTrainable params: 38,951,745\nNon-trainable params: 0\n__________________________________________________________________________________________________\n" } ], "source": [ @@ -1996,9 +1938,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.6.5-final" } }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file