Knowledge Base

Fully Connected Networks

Practice

1# Training linear regression in Keras
2# import Keras
3from tensorflow import keras
4
5# create the model
6model = keras.models.Sequential()
7# indicate how the neural network is arranged
8# units - the number of neurons in the layer
9# input_dim - the number of inputs in the layer
10model.add(keras.layers.Dense(units=1, input_dim=features.shape[1]))
11# indicate how the neural network is trained
12model.compile(loss='mean_squared_error', optimizer='sgd')
13
14# train the model
15model.fit(features, target)

1# Training logistic regression in Keras
2# import Keras
3from tensorflow import keras
4
5# create the model
6model = keras.models.Sequential()
7# indicate how the neural network is arranged
8# units - the number of neurons in the layer
9# input_dim - the number of inputs in the layer
10# activation - activation function
11model.add(keras.layers.Dense(units=1, input_dim=features_train.shape[1],
12 activation='sigmoid'))
13# indicate how the neural network is trained
14model.compile(loss='binary_crossentropy', optimizer='sgd')
15
16# train the model
17model.fit(features, target)

1# Training fully connected neural networks in Keras
2# import Keras
3from tensorflow import keras
4
5# create the model
6model = keras.models.Sequential()
7# indicate how the neural network is arranged
8# We have two layers: the first one has 10 neurons, the second one has one neuron
9# units - the number of neurons in the layer
10# input_dim - the number of inputs in the layer
11# activation - activation function
12model.add(keras.layers.Dense(units=10, input_dim=features_train.shape[1],
13 activation='sigmoid'))
14model.add(keras.layers.Dense(units=1, activation='sigmoid'))
15
16# indicate how the neural network is trained
17model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['acc'])
18
19# train the model
20model.fit(features, target)

1# Working with Images in Python
2import numpy as np
3from PIL import Image
4
5# Importing the image
6image = Image.open('image.png')
7image_array = np.array(image)
8print(image_array)
9
10# Plotting the image
11plt.imshow(image_array)
12
13# Plotting the black and white image
14plt.imshow(image_array, cmap='gray')
15
16# Adding the color bar to the image
17plt.colorbar()
Send Feedback
close
  • Bug
  • Improvement
  • Feature
Send Feedback
,