Use the buttons above to download the notebook or open it in your preferred environment.
π Notebook Preview
InΒ [2]:
importtensorflowastfimportnumpyasnpimportmatplotlib.pyplotasplt# XOR datasetX=np.array([[0,0],[0,1],[1,0],[1,1]],dtype=np.float32)y=np.array([[0],[1],[1],[0]],dtype=np.float32)print("XOR Truth Table:")print("Input\t\tOutput")foriinrange(len(X)):print(f"{X[i]}\t{y[i][0]}")print()# Build the neural network modelmodel=tf.keras.Sequential([tf.keras.layers.Dense(4,activation='relu',input_shape=(2,),name='hidden_layer'),tf.keras.layers.Dense(1,activation='sigmoid',name='output_layer')])# Compile the modelmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.1),loss='binary_crossentropy',metrics=['accuracy'])# Display model architectureprint("Model Architecture:")model.summary()print()# Train the modelprint("Training the network...")history=model.fit(X,y,epochs=500,verbose=0,batch_size=4)# Print training progressprint(f"Final Loss: {history.history['loss'][-1]:.4f}")print(f"Final Accuracy: {history.history['accuracy'][-1]:.4f}")print()# Make predictionspredictions=model.predict(X,verbose=0)print("Results after training:")print("Input\t\tExpected\tPredicted\tRounded")foriinrange(len(X)):print(f"{X[i]}\t{y[i][0]}\t\t{predictions[i][0]:.4f}\t\t{round(predictions[i][0])}")print()# Visualize decision boundarydefplot_decision_boundary(model,X,y):# Create a mesh gridx_min,x_max=-0.5,1.5y_min,y_max=-0.5,1.5h=0.01xx,yy=np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))# Predict for each point in the meshgrid_points=np.c_[xx.ravel(),yy.ravel()]Z=model.predict(grid_points,verbose=0)Z=Z.reshape(xx.shape)# Plotplt.figure(figsize=(10,8))plt.contourf(xx,yy,Z,levels=20,cmap='RdBu',alpha=0.8)plt.colorbar(label='Prediction')# Plot the XOR pointscolors=['red'iflabel==0else'blue'forlabeliny]plt.scatter(X[:,0],X[:,1],c=colors,s=200,edgecolors='black',linewidths=2)# Add labels to pointsforiinrange(len(X)):plt.annotate(f'{int(y[i][0])}',(X[i,0],X[i,1]),fontsize=14,ha='center',va='center',color='white',weight='bold')plt.xlabel('Input A',fontsize=12)plt.ylabel('Input B',fontsize=12)plt.title('XOR Problem - Decision Boundary',fontsize=14,weight='bold')plt.grid(True,alpha=0.3)plt.tight_layout()plt.savefig('xor_decision_boundary.png',dpi=150)print("Decision boundary plot saved as 'xor_decision_boundary.png'")# Plot training historydefplot_training_history(history):plt.figure(figsize=(12,4))plt.subplot(1,2,1)plt.plot(history.history['loss'])plt.title('Model Loss Over Time')plt.xlabel('Epoch')plt.ylabel('Loss')plt.grid(True,alpha=0.3)plt.subplot(1,2,2)plt.plot(history.history['accuracy'])plt.title('Model Accuracy Over Time')plt.xlabel('Epoch')plt.ylabel('Accuracy')plt.grid(True,alpha=0.3)plt.tight_layout()plt.savefig('xor_training_history.png',dpi=150)print("Training history plot saved as 'xor_training_history.png'")# Generate visualizationsplot_decision_boundary(model,X,y)plot_training_history(history)print("\nVisualization complete!")# Optional: Show weights learned by the networkprint("\nLearned Weights:")forlayerinmodel.layers:weights=layer.get_weights()print(f"\n{layer.name}:")print(f" Weights shape: {weights[0].shape}")print(f" Biases shape: {weights[1].shape}")
XOR Truth Table:
Input Output
[0. 0.] 0.0
[0. 1.] 1.0
[1. 0.] 1.0
[1. 1.] 0.0
Model Architecture: