No account yet ?
Machine learning is considered part of artificial intelligence.
Machine learning includes many algorithms:
Linear, multivariate, polynomial, regularized, logistic regressions, ... which are curves that approximate the data.
The Naïve Bayes algorithm which gives the probability of the prediction, knowing the previous events.
Clustering which, thanks to mathematics, will group the data into packets so that in each packet the data are as close as possible to each other.
Decision trees that answer a number of questions then just follow the branches of the tree to arrive at a result with a probability score.
As well as more advanced algorithms such as: Random Forest, Gradient Boosting, ...
Tested in Anaconda and Python 3.7
import pandas as pd from sklearn.cluster import MeanShift # import seaborn as sns import matplotlib.pyplot as plt colleges = pd.read_csv('College_data.csv',index_col = 0) print(colleges.info()) x = colleges[["Apps","Grad.Rate"]].values print(x) ms = MeanShift() y_hc = ms.fit_predict(x) print(y_hc) plt.scatter(x[y_hc == 0, 0], x[y_hc == 0, 1], s = 100, c = 'red', label = 'Cluster 1') plt.scatter(x[y_hc == 1, 0], x[y_hc == 1, 1], s = 100, c = 'blue', label = 'Cluster 2') plt.scatter(x[y_hc == 2, 0], x[y_hc == 2, 1], s = 100, c = 'green', label = 'Cluster 3') plt.title('University Data') plt.xlabel('Applications') plt.ylabel('Graduate Rates') plt.legend() plt.show()
Mean-shift Machine_Learning_Clustering_Algorithms - GitHub
Convolutional Neural Network (CNN)
Implementation of CNN in Python
Tested in Anaconda and Python 3.7
#importing the required libraries from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D from tensorflow.keras.layers import MaxPool2D from tensorflow.keras.layers import Flatten from tensorflow.keras.layers import Dropout from tensorflow.keras.layers import Dense #loading data (X_train,y_train) , (X_test,y_test)=mnist.load_data() #reshaping data X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)) X_test = X_test.reshape((X_test.shape[0],X_test.shape[1],X_test.shape[2],1)) #checking the shape after reshaping print(X_train.shape) print(X_test.shape) #normalizing the pixel values X_train=X_train/255 X_test=X_test/255 #defining model model=Sequential() #adding convolution layer model.add(Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1))) #adding pooling layer model.add(MaxPool2D(2,2)) #adding fully connected layer model.add(Flatten()) model.add(Dense(100,activation='relu')) #adding output layer model.add(Dense(10,activation='softmax')) #compiling the model model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy']) #fitting the model model.fit(X_train,y_train,epochs=10) #evaluting the model model.evaluate(X_test,y_test)
Source : https://www.analyticsvidhya.com/blog/2021/08/beginners-guide-to-convolutional-neural-network-with-implementation-in-python/
Result
Epoch 1/10
60000/60000 [==============================] - 9s 151us/sample - loss: 0.1630 - acc: 0.9508
Epoch 2/10
60000/60000 [==============================] - 7s 120us/sample - loss: 0.0533 - acc: 0.9837
Epoch 3/10
60000/60000 [==============================] - 7s 123us/sample - loss: 0.0356 - acc: 0.9891
Epoch 4/10
60000/60000 [==============================] - 7s 122us/sample - loss: 0.0242 - acc: 0.9926
Epoch 5/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.0172 - acc: 0.9945
Epoch 6/10
60000/60000 [==============================] - 8s 133us/sample - loss: 0.0117 - acc: 0.9961
Epoch 7/10
60000/60000 [==============================] - 8s 125us/sample - loss: 0.0100 - acc: 0.9969
Epoch 8/10
60000/60000 [==============================] - 7s 118us/sample - loss: 0.0068 - acc: 0.9977
Epoch 9/10
60000/60000 [==============================] - 8s 125us/sample - loss: 0.0054 - acc: 0.9983
Epoch 10/10
60000/60000 [==============================] - 7s 119us/sample - loss: 0.0051 - acc: 0.9983
10000/10000 [==============================] - 1s 65us/sample - loss: 0.0551 - acc: 0.9875
Artificial Neural Network (ANN)
Implementation of ANN in Python
Tested in Anaconda and Python 3.7
#importing libraries import numpy as np import matplotlib.pyplot as plt import seaborn as sns # Ignore the warnings import warnings warnings.filterwarnings("ignore") #loading MNIST dataset from tensorflow.keras.datasets import mnist (X_train,y_train) , (X_test,y_test)=mnist.load_data() #visualizing the image in train data plt.imshow(X_train[0]) #visualizing the first 20 images in the dataset for i in range(25): #subplot plt.subplot(5, 5, i+1) # plotting pixel data plt.imshow(X_train[i], cmap=plt.get_cmap('gray')) # show the figure plt.show() print(X_train.shape) print(X_test.shape) # the image is in pixels which ranges from 0 to 255 X_train[0] X_train_flat=X_train.reshape(len(X_train),28*28) X_test_flat=X_test.reshape(len(X_test),28*28) #checking the shape after flattening print(X_train_flat.shape) print(X_test_flat.shape) #checking the representation of image after flattening X_train_flat[0] #normalizing the pixel values X_train_flat=X_train_flat/255 X_test_flat=X_test_flat/255 #print this code to check the pixel values after normalization X_train_flat[0] #Building a simple ANN model without hidden layer #importing necessary libraries from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense #Step 1 : Defining the model model=Sequential() model.add(Dense(10,input_shape=(784,),activation='softmax')) #Step 2: Compiling the model model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy']) #Step 3: Fitting the model model.fit(X_train_flat,y_train,epochs=10) #Step 4: Evaluating the model model.evaluate(X_test_flat,y_test) #Step 5 :Making predictions y_predict = model.predict(X_test_flat) y_predict[3] #printing the 3rd index # Here we get the index of the maximum value in the above-encoded vector. np.argmax(y_predict[3]) #checking if the predicting is correct plt.imshow(X_test[3]) y_predict_labels=np.argmax(y_predict,axis=1) #Confusion matrix from sklearn.metrics import confusion_matrix matrix=confusion_matrix(y_test,y_predict_labels) #visualizaing confusion matrix with heatmap plt.figure(figsize=(10,7)) sns.heatmap(matrix,annot=True,fmt='d') model2=Sequential() #adding first layer with 100 neurons model2.add(Dense(100,input_shape=(784,),activation='relu')) #second layer with 64 neurons model2.add(Dense(64,activation='relu')) #third layer with 32 neurons model2.add(Dense(32,activation='relu')) #output layer model2.add(Dense(10,activation='softmax')) #compliling the model model2.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy']) #fitting the model model2.fit(X_train_flat,y_train,epochs=10) #evaluating the model model2.evaluate(X_test_flat,y_test)
Source : https://www.analyticsvidhya.com/blog/2021/08/implementing-artificial-neural-network-on-unstructured-data/
Result
Epoch 1/10
60000/60000 [==============================] - 4s 70us/sample - loss: 0.4720 - acc: 0.8763
Epoch 2/10
60000/60000 [==============================] - 4s 66us/sample - loss: 0.3036 - acc: 0.9160
Epoch 3/10
60000/60000 [==============================] - 4s 70us/sample - loss: 0.2829 - acc: 0.9215
Epoch 4/10
60000/60000 [==============================] - 4s 67us/sample - loss: 0.2733 - acc: 0.9234
Epoch 5/10
60000/60000 [==============================] - 4s 67us/sample - loss: 0.2669 - acc: 0.9256
Epoch 6/10
60000/60000 [==============================] - 4s 67us/sample - loss: 0.2623 - acc: 0.9269
Epoch 7/10
60000/60000 [==============================] - 4s 70us/sample - loss: 0.2585 - acc: 0.9285
Epoch 8/10
60000/60000 [==============================] - 4s 70us/sample - loss: 0.2554 - acc: 0.9295 3s - loss: 0.2484 - acc: 0.9305
Epoch 9/10
60000/60000 [==============================] - 4s 69us/sample - loss: 0.2528 - acc: 0.9304
Epoch 10/10
60000/60000 [==============================] - 4s 71us/sample - loss: 0.2509 - acc: 0.9304
10000/10000 [==============================] - 0s 49us/sample - loss: 0.2653 - acc: 0.9268
Epoch 1/10
60000/60000 [==============================] - 6s 102us/sample - loss: 0.2697 - acc: 0.9196
Epoch 2/10
60000/60000 [==============================] - 6s 94us/sample - loss: 0.1174 - acc: 0.9646
Epoch 3/10
60000/60000 [==============================] - 6s 100us/sample - loss: 0.0847 - acc: 0.9736
Epoch 4/10
60000/60000 [==============================] - 6s 98us/sample - loss: 0.0692 - acc: 0.9783
Epoch 5/10
60000/60000 [==============================] - 6s 99us/sample - loss: 0.0542 - acc: 0.9829: 5s - loss: 0.0581 - acc: 0.9826
Epoch 6/10
60000/60000 [==============================] - 6s 106us/sample - loss: 0.0457 - acc: 0.9854
Epoch 7/10
60000/60000 [==============================] - 6s 100us/sample - loss: 0.0396 - acc: 0.9865
Epoch 8/10
60000/60000 [==============================] - 6s 97us/sample - loss: 0.0327 - acc: 0.9896
Epoch 9/10
60000/60000 [==============================] - 6s 99us/sample - loss: 0.0302 - acc: 0.9898
Epoch 10/10
60000/60000 [==============================] - 6s 96us/sample - loss: 0.0260 - acc: 0.991860000 [==========>...................] - ETA: 3s - loss: 0.0193 - acc: 0.9935
10000/10000 [==============================] - 1s 55us/sample - loss: 0.0920 - acc: 0.9768
Developing an Image Classification Model Using CNN
Tested in Anaconda and Python 3.7
# importing necessary libraries import numpy as np import matplotlib.pyplot as plt # To convert to categorical data from tensorflow.keras.utils import to_categorical #libraries for building model from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Dropout,Flatten from tensorflow.keras.datasets import cifar10 #loading the data (X_train, y_train), (X_test, y_test) = cifar10.load_data() #shape of the dataset print(X_train.shape) print(y_train.shape) print(X_test.shape) print(y_test.shape) #checking the labels np.unique(y_train) #first image of training data plt.subplot(121) plt.imshow(X_train[0]) plt.show() plt.title("Label : {}".format(y_train[0])) #first image of test data plt.subplot(122) plt.imshow(X_test[0]) plt.show() plt.title("Label : {}".format(y_test[0])); #visualizing the first 20 images in the dataset for i in range(20): #subplot plt.subplot(5, 5, i+1) # plotting pixel data plt.imshow(X_train[i], cmap=plt.get_cmap('gray')) # show the figure plt.show() # Scale the data to lie between 0 to 1 X_train = X_train/255 X_test = X_test/255 print(X_train) #reshaping the train and test lables to 1D y_train = y_train.reshape(-1,) y_test = y_test.reshape(-1,) model=Sequential() #adding the first Convolution layer model.add(Conv2D(32,(3,3),activation='relu',input_shape=(32,32,3))) #adding Max pooling layer model.add(MaxPool2D(2,2)) #adding another Convolution layer model.add(Conv2D(64,(3,3),activation='relu')) model.add(MaxPool2D(2,2)) model.add(Flatten()) #adding dense layer model.add(Dense(216,activation='relu')) #adding output layer model.add(Dense(10,activation='softmax')) model.compile(optimizer='rmsprop',loss='sparse_categorical_crossentropy',metrics=['accuracy']) model.fit(X_train,y_train,epochs=10) model.evaluate(X_test,y_test) pred=model.predict(X_test) #printing the first element from predicted data print(pred[0]) #printing the index of print('Index:',np.argmax(pred[0])) y_classes = [np.argmax(element) for element in pred] print('Predicted_values:',y_classes[:10]) print('Actual_values:',y_test[:10]) model4=Sequential() #adding the first Convolution layer model4.add(Conv2D(32,(3,3),activation='relu',input_shape=(32,32,3))) #adding Max pooling layer model4.add(MaxPool2D(2,2)) #adding dropout model4.add(Dropout(0.2)) #adding another Convolution layer model4.add(Conv2D(64,(3,3),activation='relu')) model4.add(MaxPool2D(2,2)) #adding dropout model4.add(Dropout(0.2)) model4.add(Flatten()) #adding dense layer model4.add(Dense(216,activation='relu')) #adding dropout model4.add(Dropout(0.2)) #adding output layer model4.add(Dense(10,activation='softmax')) model4.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy']) model4.fit(X_train,y_train,epochs=10) model4.evaluate(X_test,y_test)
Source : https://www.analyticsvidhya.com/blog/2021/08/developing-an-image-classification-model-using-cnn/
Result
(50000, 32, 32, 3)
(50000, 1)
(10000, 32, 32, 3)
(10000, 1)
[[[[0.23137255 0.24313725 0.24705882]
[0.16862745 0.18039216 0.17647059]
[0.19607843 0.18823529 0.16862745]
...
[0.61960784 0.51764706 0.42352941]
[0.59607843 0.49019608 0.4 ]
[0.58039216 0.48627451 0.40392157]]
[[0.0627451 0.07843137 0.07843137]
[0. 0. 0. ]
[0.07058824 0.03137255 0. ]
...
[0.48235294 0.34509804 0.21568627]
[0.46666667 0.3254902 0.19607843]
[0.47843137 0.34117647 0.22352941]]
[[0.09803922 0.09411765 0.08235294]
[0.0627451 0.02745098 0. ]
[0.19215686 0.10588235 0.03137255]
...
[0.4627451 0.32941176 0.19607843]
[0.47058824 0.32941176 0.19607843]
[0.42745098 0.28627451 0.16470588]]
...
[[0.81568627 0.66666667 0.37647059]
[0.78823529 0.6 0.13333333]
[0.77647059 0.63137255 0.10196078]
...
[0.62745098 0.52156863 0.2745098 ]
[0.21960784 0.12156863 0.02745098]
[0.20784314 0.13333333 0.07843137]]
[[0.70588235 0.54509804 0.37647059]
[0.67843137 0.48235294 0.16470588]
[0.72941176 0.56470588 0.11764706]
...
[0.72156863 0.58039216 0.36862745]
[0.38039216 0.24313725 0.13333333]
[0.3254902 0.20784314 0.13333333]]
[[0.69411765 0.56470588 0.45490196]
[0.65882353 0.50588235 0.36862745]
[0.70196078 0.55686275 0.34117647]
...
[0.84705882 0.72156863 0.54901961]
[0.59215686 0.4627451 0.32941176]
[0.48235294 0.36078431 0.28235294]]]
[[[0.60392157 0.69411765 0.73333333]
[0.49411765 0.5372549 0.53333333]
[0.41176471 0.40784314 0.37254902]
...
[0.35686275 0.37254902 0.27843137]
[0.34117647 0.35294118 0.27843137]
[0.30980392 0.31764706 0.2745098 ]]
[[0.54901961 0.62745098 0.6627451 ]
[0.56862745 0.6 0.60392157]
[0.49019608 0.49019608 0.4627451 ]
...
[0.37647059 0.38823529 0.30588235]
[0.30196078 0.31372549 0.24313725]
[0.27843137 0.28627451 0.23921569]]
[[0.54901961 0.60784314 0.64313725]
[0.54509804 0.57254902 0.58431373]
[0.45098039 0.45098039 0.43921569]
...
[0.30980392 0.32156863 0.25098039]
[0.26666667 0.2745098 0.21568627]
[0.2627451 0.27058824 0.21568627]]
...
[[0.68627451 0.65490196 0.65098039]
[0.61176471 0.60392157 0.62745098]
[0.60392157 0.62745098 0.66666667]
...
[0.16470588 0.13333333 0.14117647]
[0.23921569 0.20784314 0.22352941]
[0.36470588 0.3254902 0.35686275]]
[[0.64705882 0.60392157 0.50196078]
[0.61176471 0.59607843 0.50980392]
[0.62352941 0.63137255 0.55686275]
...
[0.40392157 0.36470588 0.37647059]
[0.48235294 0.44705882 0.47058824]
[0.51372549 0.4745098 0.51372549]]
[[0.63921569 0.58039216 0.47058824]
[0.61960784 0.58039216 0.47843137]
[0.63921569 0.61176471 0.52156863]
...
[0.56078431 0.52156863 0.54509804]
[0.56078431 0.5254902 0.55686275]
[0.56078431 0.52156863 0.56470588]]]
[[[1. 1. 1. ]
[0.99215686 0.99215686 0.99215686]
[0.99215686 0.99215686 0.99215686]
...
[0.99215686 0.99215686 0.99215686]
[0.99215686 0.99215686 0.99215686]
[0.99215686 0.99215686 0.99215686]]
[[1. 1. 1. ]
[1. 1. 1. ]
[1. 1. 1. ]
...
[1. 1. 1. ]
[1. 1. 1. ]
[1. 1. 1. ]]
[[1. 1. 1. ]
[0.99607843 0.99607843 0.99607843]
[0.99607843 0.99607843 0.99607843]
...
[0.99607843 0.99607843 0.99607843]
[0.99607843 0.99607843 0.99607843]
[0.99607843 0.99607843 0.99607843]]
...
[[0.44313725 0.47058824 0.43921569]
[0.43529412 0.4627451 0.43529412]
[0.41176471 0.43921569 0.41568627]
...
[0.28235294 0.31764706 0.31372549]
[0.28235294 0.31372549 0.30980392]
[0.28235294 0.31372549 0.30980392]]
[[0.43529412 0.4627451 0.43137255]
[0.40784314 0.43529412 0.40784314]
[0.38823529 0.41568627 0.38431373]
...
[0.26666667 0.29411765 0.28627451]
[0.2745098 0.29803922 0.29411765]
[0.30588235 0.32941176 0.32156863]]
[[0.41568627 0.44313725 0.41176471]
[0.38823529 0.41568627 0.38431373]
[0.37254902 0.4 0.36862745]
...
[0.30588235 0.33333333 0.3254902 ]
[0.30980392 0.33333333 0.3254902 ]
[0.31372549 0.3372549 0.32941176]]]
...
[[[0.1372549 0.69803922 0.92156863]
[0.15686275 0.69019608 0.9372549 ]
[0.16470588 0.69019608 0.94509804]
...
[0.38823529 0.69411765 0.85882353]
[0.30980392 0.57647059 0.77254902]
[0.34901961 0.58039216 0.74117647]]
[[0.22352941 0.71372549 0.91764706]
[0.17254902 0.72156863 0.98039216]
[0.19607843 0.71764706 0.94117647]
...
[0.61176471 0.71372549 0.78431373]
[0.55294118 0.69411765 0.80784314]
[0.45490196 0.58431373 0.68627451]]
[[0.38431373 0.77254902 0.92941176]
[0.25098039 0.74117647 0.98823529]
[0.27058824 0.75294118 0.96078431]
...
[0.7372549 0.76470588 0.80784314]
[0.46666667 0.52941176 0.57647059]
[0.23921569 0.30980392 0.35294118]]
...
[[0.28627451 0.30980392 0.30196078]
[0.20784314 0.24705882 0.26666667]
[0.21176471 0.26666667 0.31372549]
...
[0.06666667 0.15686275 0.25098039]
[0.08235294 0.14117647 0.2 ]
[0.12941176 0.18823529 0.19215686]]
[[0.23921569 0.26666667 0.29411765]
[0.21568627 0.2745098 0.3372549 ]
[0.22352941 0.30980392 0.40392157]
...
[0.09411765 0.18823529 0.28235294]
[0.06666667 0.1372549 0.20784314]
[0.02745098 0.09019608 0.1254902 ]]
[[0.17254902 0.21960784 0.28627451]
[0.18039216 0.25882353 0.34509804]
[0.19215686 0.30196078 0.41176471]
...
[0.10588235 0.20392157 0.30196078]
[0.08235294 0.16862745 0.25882353]
[0.04705882 0.12156863 0.19607843]]]
[[[0.74117647 0.82745098 0.94117647]
[0.72941176 0.81568627 0.9254902 ]
[0.7254902 0.81176471 0.92156863]
...
[0.68627451 0.76470588 0.87843137]
[0.6745098 0.76078431 0.87058824]
[0.6627451 0.76078431 0.8627451 ]]
[[0.76078431 0.82352941 0.9372549 ]
[0.74901961 0.81176471 0.9254902 ]
[0.74509804 0.80784314 0.92156863]
...
[0.67843137 0.75294118 0.8627451 ]
[0.67058824 0.74901961 0.85490196]
[0.65490196 0.74509804 0.84705882]]
[[0.81568627 0.85882353 0.95686275]
[0.80392157 0.84705882 0.94117647]
[0.8 0.84313725 0.9372549 ]
...
[0.68627451 0.74901961 0.85098039]
[0.6745098 0.74509804 0.84705882]
[0.6627451 0.74901961 0.84313725]]
...
[[0.81176471 0.78039216 0.70980392]
[0.79607843 0.76470588 0.68627451]
[0.79607843 0.76862745 0.67843137]
...
[0.52941176 0.51764706 0.49803922]
[0.63529412 0.61960784 0.58823529]
[0.65882353 0.63921569 0.59215686]]
[[0.77647059 0.74509804 0.66666667]
[0.74117647 0.70980392 0.62352941]
[0.70588235 0.6745098 0.57647059]
...
[0.69803922 0.67058824 0.62745098]
[0.68627451 0.6627451 0.61176471]
[0.68627451 0.6627451 0.60392157]]
[[0.77647059 0.74117647 0.67843137]
[0.74117647 0.70980392 0.63529412]
[0.69803922 0.66666667 0.58431373]
...
[0.76470588 0.72156863 0.6627451 ]
[0.76862745 0.74117647 0.67058824]
[0.76470588 0.74509804 0.67058824]]]
[[[0.89803922 0.89803922 0.9372549 ]
[0.9254902 0.92941176 0.96862745]
[0.91764706 0.9254902 0.96862745]
...
[0.85098039 0.85882353 0.91372549]
[0.86666667 0.8745098 0.91764706]
[0.87058824 0.8745098 0.91372549]]
[[0.87058824 0.86666667 0.89803922]
[0.9372549 0.9372549 0.97647059]
[0.91372549 0.91764706 0.96470588]
...
[0.8745098 0.8745098 0.9254902 ]
[0.89019608 0.89411765 0.93333333]
[0.82352941 0.82745098 0.8627451 ]]
[[0.83529412 0.80784314 0.82745098]
[0.91764706 0.90980392 0.9372549 ]
[0.90588235 0.91372549 0.95686275]
...
[0.8627451 0.8627451 0.90980392]
[0.8627451 0.85882353 0.90980392]
[0.79215686 0.79607843 0.84313725]]
...
[[0.58823529 0.56078431 0.52941176]
[0.54901961 0.52941176 0.49803922]
[0.51764706 0.49803922 0.47058824]
...
[0.87843137 0.87058824 0.85490196]
[0.90196078 0.89411765 0.88235294]
[0.94509804 0.94509804 0.93333333]]
[[0.5372549 0.51764706 0.49411765]
[0.50980392 0.49803922 0.47058824]
[0.49019608 0.4745098 0.45098039]
...
[0.70980392 0.70588235 0.69803922]
[0.79215686 0.78823529 0.77647059]
[0.83137255 0.82745098 0.81176471]]
[[0.47843137 0.46666667 0.44705882]
[0.4627451 0.45490196 0.43137255]
[0.47058824 0.45490196 0.43529412]
...
[0.70196078 0.69411765 0.67843137]
[0.64313725 0.64313725 0.63529412]
[0.63921569 0.63921569 0.63137255]]]]
Epoch 1/10
50000/50000 [==============================] - 9s 177us/sample - loss: 1.4020 - acc: 0.4998
Epoch 2/10
50000/50000 [==============================] - 9s 174us/sample - loss: 1.0189 - acc: 0.6459
Epoch 3/10
50000/50000 [==============================] - 9s 179us/sample - loss: 0.8563 - acc: 0.7043
Epoch 4/10
50000/50000 [==============================] - 9s 178us/sample - loss: 0.7378 - acc: 0.7441
Epoch 5/10
50000/50000 [==============================] - 9s 178us/sample - loss: 0.6414 - acc: 0.7827
Epoch 6/10
50000/50000 [==============================] - 9s 180us/sample - loss: 0.5610 - acc: 0.8091
Epoch 7/10
50000/50000 [==============================] - 9s 182us/sample - loss: 0.4938 - acc: 0.8328
Epoch 8/10
50000/50000 [==============================] - 9s 180us/sample - loss: 0.4329 - acc: 0.8536
Epoch 9/10
50000/50000 [==============================] - 9s 180us/sample - loss: 0.3846 - acc: 0.8702
Epoch 10/10
50000/50000 [==============================] - 9s 177us/sample - loss: 0.3494 - acc: 0.8830 7s - loss: 0.3079 - acc: 0.8981
10000/10000 [==============================] - 1s 98us/sample - loss: 1.3305 - acc: 0.6932
[8.3880089e-08 1.5042266e-05 1.8243658e-04 9.4401753e-01 3.9289816e-06
5.5519883e-02 2.3185095e-05 1.3974671e-04 7.1556511e-05 2.6683236e-05]
Index: 3
Predicted_values: [3, 8, 8, 0, 4, 6, 1, 6, 3, 1]
Actual_values: [3 8 8 0 6 6 1 6 3 1]
Epoch 1/10
50000/50000 [==============================] - 9s 187us/sample - loss: 1.5135 - acc: 0.4515
Epoch 2/10
50000/50000 [==============================] - 9s 185us/sample - loss: 1.1761 - acc: 0.5828
Epoch 3/10
50000/50000 [==============================] - 9s 190us/sample - loss: 1.0472 - acc: 0.6301
Epoch 4/10
50000/50000 [==============================] - 9s 187us/sample - loss: 0.9552 - acc: 0.6656
Epoch 5/10
50000/50000 [==============================] - 9s 185us/sample - loss: 0.8951 - acc: 0.6849
Epoch 6/10
50000/50000 [==============================] - 9s 182us/sample - loss: 0.8385 - acc: 0.7042
Epoch 7/10
50000/50000 [==============================] - 9s 179us/sample - loss: 0.7897 - acc: 0.7204
Epoch 8/10
50000/50000 [==============================] - 9s 183us/sample - loss: 0.7491 - acc: 0.7361
Epoch 9/10
50000/50000 [==============================] - 9s 180us/sample - loss: 0.7141 - acc: 0.7452
Epoch 10/10
50000/50000 [==============================] - 9s 181us/sample - loss: 0.6836 - acc: 0.7577
10000/10000 [==============================] - 1s 101us/sample - loss: 0.7977 - acc: 0.7260
Tested in Anaconda and Python 3.7
# -*- coding: utf-8 -*- """ Created on Thu Jun 16 20:59:14 2022 @author: Tensorflow : https://www.tensorflow.org/tutorials/images/cnn """ #Importer TensorFlow import tensorflow as tf from tensorflow.keras import datasets, layers, models import matplotlib.pyplot as plt #Télécharger et préparer le jeu de données CIFAR10 (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data() # Normalize pixel values to be between 0 and 1 train_images, test_images = train_images / 255.0, test_images / 255.0 #Vérifier les données class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i]) # The CIFAR labels happen to be arrays, # which is why you need the extra index plt.xlabel(class_names[train_labels[i][0]]) plt.show() #Créer la base convolutive model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) #Affichons l'architecture de votre modèle jusqu'à présent : model.summary() #Ajouter des couches denses sur le dessus model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(10)) #Voici l'architecture complète de votre modèle : model.summary() #Compiler et entraîner le modèle model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['acc']) history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels)) #Évaluer le modèle plt.plot(history.history['acc'], label='Accuracy') plt.plot(history.history['val_acc'], label = 'val_accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.ylim([0.5, 1]) plt.legend(loc='lower right') test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) print(test_acc)
Result
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 30, 30, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 13, 13, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 4, 4, 64) 36928
=================================================================
Total params: 56,320
Trainable params: 56,320
Non-trainable params: 0
_________________________________________________________________
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 30, 30, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 13, 13, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 4, 4, 64) 36928
_________________________________________________________________
flatten (Flatten) (None, 1024) 0
_________________________________________________________________
dense (Dense) (None, 64) 65600
_________________________________________________________________
dense_1 (Dense) (None, 10) 650
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
_________________________________________________________________
Train on 50000 samples, validate on 10000 samples
50000/50000 [==============================] - 12s 238us/sample - loss: 1.5028 - acc: 0.4556 - val_loss: 1.4155 - val_acc: 0.5131
Epoch 2/10
50000/50000 [==============================] - 10s 196us/sample - loss: 1.1316 - acc: 0.6003 - val_loss: 1.0506 - val_acc: 0.6277
Epoch 3/10
50000/50000 [==============================] - 10s 196us/sample - loss: 0.9700 - acc: 0.6582 - val_loss: 0.9494 - val_acc: 0.6670
Epoch 4/10
50000/50000 [==============================] - 10s 201us/sample - loss: 0.8795 - acc: 0.6915 - val_loss: 0.9110 - val_acc: 0.6802
Epoch 5/10
50000/50000 [==============================] - 10s 196us/sample - loss: 0.8057 - acc: 0.7171 - val_loss: 0.8866 - val_acc: 0.6960
Epoch 6/10
50000/50000 [==============================] - 10s 193us/sample - loss: 0.7477 - acc: 0.7395 - val_loss: 0.8815 - val_acc: 0.6996
Epoch 7/10
50000/50000 [==============================] - 10s 197us/sample - loss: 0.7029 - acc: 0.7526 - val_loss: 0.8872 - val_acc: 0.6995
Epoch 8/10
50000/50000 [==============================] - 10s 207us/sample - loss: 0.6562 - acc: 0.7691 - val_loss: 0.8546 - val_acc: 0.7127
Epoch 9/10
50000/50000 [==============================] - 10s 192us/sample - loss: 0.6168 - acc: 0.7830 - val_loss: 0.8714 - val_acc: 0.7140
Epoch 10/10
50000/50000 [==============================] - 10s 198us/sample - loss: 0.5775 - acc: 0.7952 - val_loss: 0.8378 - val_acc: 0.7233
10000/10000 - 1s - loss: 0.8378 - acc: 0.7233
0.7233
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License