We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
1.Simple Vector addition 2.Regression model 3.Implement Perceptron 4.
Implement feedforwared network
# importing packages import tensorflow as tf #exp4 Implement a feed forward network in tensorflow/keras import numpy as np import tensorflow as tf from tensorflow import keras import tensorflow as tf from keras.models import Sequential # creating a scalar from tensorflow.keras.layers import Dense from tensorflow import keras from keras.layers import Dense scalar = tf.constant(7) from tensorflow.keras.optimizers import SGD from tensorflow.keras.layers import Dense from sklearn.model_selection import train_test_split scalar import numpy as np import numpy as np from sklearn.metrics import mean_squared_error scalar.ndim # Generate some sample data for a logical OR operation # Generate some sample data import matplotlib.pyplot as plt # create a vector X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # Input features X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # Input features np.random.seed(42) vector = tf.constant([10, 10]) y = np.array([0, 1, 1, 1]) # Output labels (OR gate) y = np.array([0, 1, 1, 0]) # Output labels (XOR gate) X = np.random.rand(100, 1) # checking the dimensions of vector # Define a simple perceptron model # Define a feedforward neural network model y = 3 * X + 2 + 0.1 * np.random.randn(100, 1) vector.ndim model = keras.Sequential([ model = keras.Sequential([ # Split the data into training and testing sets # creating a matrix Dense(units=1, input_dim=2, activation='sigmoid') # 2 input features, 1 Dense(units=4, input_dim=2, activation='relu'), # 2 input features, 4 hidden X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, matrix = tf.constant([[1, 2], [3, 4]]) output unit, sigmoid activation units with ReLU activation random_state=42) print(matrix) ]) Dense(units=1, activation='sigmoid') # 1 output unit with sigmoid # Build the regression model print('the number of dimensions of a matrix is : ' +str(matrix.ndim)) # Compile the model activation model = Sequential() # creating two tensors model.compile(optimizer=SGD(learning_rate=0.1), ]) model.add(Dense(units=10, input_dim=1, activation='relu')) # Hidden layer matrix = tf.constant([[1, 2], [3, 4]]) loss='mean_squared_error', metrics=['accuracy']) # Compile the model with ReLU activation matrix1 = tf.constant([[2, 4], [6, 8]]) # Train the model model.compile(optimizer='adam', loss='binary_crossentropy', model.add(Dense(units=1)) # Output layer # addition of two matrices model.fit(X, y, epochs=1000, verbose=0) # You can adjust the number of metrics=['accuracy']) # Compile the model print(matrix+matrix1) epochs # Train the model model.compile(optimizer='adam', loss='mean_squared_error') # subtraction of two matrices # Evaluate the model model.fit(X, y, epochs=1000, verbose=0) # You can adjust the number of # Train the model print(matrix1 - matrix) loss, accuracy = model.evaluate(X, y) epochs model.fit(X_train, y_train, epochs=10, batch_size=8, verbose=1, # multiplication of two matrices print("Loss:", loss) # Evaluate the model validation_split=0.2) print(matrix1 * matrix) print("Accuracy:", accuracy) loss, accuracy = model.evaluate(X, y) # Evaluate the model on the test set # division of two matrices # Make predictions print("Loss:", loss) y_pred = model.predict(X_test) print(matrix1 / matrix) predictions = model.predict(X) print("Accuracy:", accuracy) # Calculate and print the mean squared error # creating a matrix print("Predictions:") # Make predictions mse = mean_squared_error(y_test, y_pred) matrix = tf.constant([[1, 2], [3, 4]]) print(predictions) predictions = model.predict(X) print("Mean Squared Error:", mse) print(matrix) print("Predictions:") # Plot the results # transpose of the matrix print(predictions) plt.scatter(X_test, y_test, label='True data') print(tf.transpose(matrix)) plt.scatter(X_test, y_pred, label='Predicted data') # dot product of matrices plt.xlabel('X') print('dot product of matrices is : ' +str(tf.tensordot(matrix, matrix, plt.ylabel('y') axes=1))) plt.legend() plt.show()