0% found this document useful (0 votes)
18 views12 pages

Deep Learning for Solving Equations

Uploaded by

953623243030
Copyright
© All Rights Reserved
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
0% found this document useful (0 votes)
18 views12 pages

Deep Learning for Solving Equations

Uploaded by

953623243030
Copyright
© All Rights Reserved
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

EX NO : 8

Solving Mathematical Equation using DeepLearning


Date:

Aim:
To develop a deep learning model capable of automatically understanding and
solving mathematical equations by learning from data rather than traditional rule
based programming.

Objectives:
1. To explore how deep learning models can interpret and solve different types
of mathematical equations.
2. To train a neural network using datasets of equations and their corresponding
solutions.
3. To compare the performance of the deep learning approach with traditional
symbolic or numerical solvers.

About the Project:


Traditional equation-solving methods rely on mathematical rules and symbolic
computation (like MATLAB, Wolfram Alpha, or SymPy). However, deep learning
provides a data-driven approach — where models learn patterns from examples
rather than explicit formulas.
In this project, a neural network (e.g., LSTM, Transformer, or Seq2Seq model) is
trained on a dataset containing mathematical equations and their solutions.

Key Concepts:
1. Deep Learning: A subset of machine learning that uses neural networks
with multiple layers to learn complex patterns.
2. Sequence-to-Sequence Model (Seq2Seq): Often used for equation-to
solution mapping, similar to language translation.
3. Recurrent Neural Networks (RNNs) / LSTMs: Help process sequential
data like mathematical expressions.

Observations:
• The model can learn to solve simple equations efficiently when trained with
sufficient examples.
• Accuracy decreases as equation complexity (like higher-order polynomials or
multiple variables) increases.
• Data quality and diversity greatly affect performance.
• Transformer-based models (like GPT or BERT variants) perform better than
basic RNNs due to better context handling.
• The system performs well on patterns it has seen but struggles with unseen or
logically complex equations.
Advantages:
1. Can learn and predict without explicit programming of mathematical rules. 2.
Useful in educational tools for automated equation solving and step-by-step
explanations.
3. Can handle noisy or handwritten inputs when combined with image
recognition.

Disadvantages:
1. Requires large and diverse datasets for accurate learning.
2. May not always guarantee mathematically correct or logical results. 3. Lack
of interpretability — the model’s internal reasoning is often a “black box.”

Program:
CNN Sequence Model:
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
import numpy as np
import cv2
import json
import os
class CNNFeatureExtractor:
def __init__(self, img_width=280, img_height=70):
self.img_width = img_width
self.img_height = img_height
[Link] = None
def build_model(self):
inputs = [Link](shape=(self.img_height, self.img_width, 1), name='image_input') x
= layers.Conv2D(32, (3, 3), activation='relu', padding='same')(inputs) x =
layers.MaxPooling2D((2, 2))(x)
x = [Link]()(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2))(x)
x = [Link]()(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2))(x)
x = [Link]()(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = [Link]()(x)
x = [Link]((2, 1, 3))(x)
shape = [Link]
# Reshape: combine height and channels
target_shape = (shape[1], shape[2] * shape[3])
x = [Link](target_shape=target_shape)(x)
x = [Link](128, activation='relu')(x)
x = [Link](0.2)(x)
outputs = x
[Link] = [Link](inputs=inputs, outputs=outputs,
name='CNN_Feature_Extractor')

print("✓ CNN Model built successfully!")


[Link]()

return [Link]
def preprocess_image(self, image_path)

img = [Link](image_path, cv2.IMREAD_GRAYSCALE)

if img is None:
raise ValueError(f"Could not read image: {image_path}"
img = [Link](img, (self.img_width, self.img_height)) img
= [Link](np.float32) / 255.
img = np.expand_dims(img, axis=-1)

return img
def load_dataset(self, dataset_dir='math_dataset'):
print("Loading dataset...")

info_path = [Link](dataset_dir, 'dataset_info.json')


with open(info_path, 'r') as f:
dataset_info = [Link](f)
images = []
labels = []

for item in dataset_info:


img_path = [Link](dataset_dir, 'images', item['image'])
img = self.preprocess_image(img_path)
[Link](img)
[Link](item['labels'])

images = [Link](images)
print(f"✓ Loaded {len(images)} images")
print(f"✓ Image shape: {images[0].shape}")

return images, labels, dataset_info


def save_model(self, filepath='models/cnn_extractor.h5'):
[Link]([Link](filepath), exist_ok=True)
[Link](filepath)
print(f"✓ Model saved to: {filepath}")
def load_model(self, filepath='models/cnn_extractor.h5'):
"""Load a saved CNN model"""
[Link] = [Link].load_model(filepath)
print(f"✓ Model loaded from: {filepath}")
return [Link]
def test_cnn():
print("="*60)
print("Testing CNN Feature Extractor")
print("="*60)
cnn = CNNFeatureExtractor()
model = cnn.build_model()
images, labels, dataset_info = cnn.load_dataset('math_dataset')

print("\nTesting feature extraction with first image...")


test_img = np.expand_dims(images[0], axis=0)
features = [Link](test_img, verbose=0)
print(f"✓ Input shape: {test_img.shape}")
print(f"✓ Output features shape: {[Link]}")
print(f"✓ Equation: {dataset_info[0]['equation']}")
cnn.save_model()
print("\n" + "="*60)
print("CNN Feature Extractor is ready!")
print("Next step: Build the RNN Sequence Model")
print("="*60)
return cnn, images, labels, dataset_info
if __name__ == '__main__':
test_cnn()

Output:
Testing CNN Feature Extractor
Model: "CNN_Feature_Extractor"
Layer (type) Output Shape Param # image_input
(InputLayer) [(None, 70, 280, 1)] 0
conv2d (Conv2D) (None, 70, 280, 32) 320
max_pooling2d (MaxPooling2 (None, 35, 140, 32) 0 D)

batch_normalization (Batch (None, 35, 140, 32) 128


Normalization)

conv2d_1 (Conv2D) (None, 35, 140, 64) 18496


max_pooling2d_1 (MaxPoolin (None, 17, 70, 64) 0 g2D)

batch_normalization_1 (Bat (None, 17, 70, 64) 256


chNormalization)
conv2d_2 (Conv2D) (None, 17, 70, 128) 73856
max_pooling2d_2 (MaxPoolin (None, 8, 35, 128) 0 g2D)

batch_normalization_2 (Bat (None, 8, 35, 128) 512


chNormalization)

conv2d_3 (Conv2D) (None, 8, 35, 256) 295168


batch_normalization_3 (Bat (None, 8, 35, 256) 1024
chNormalization)

permute (Permute) (None, 35, 8, 256) 0


reshape (Reshape) (None, 35, 2048) 0
dense (Dense) (None, 35, 128) 262272
dropout (Dropout) (None, 35, 128) 0 Total params:
652032 (2.49 MB)
Trainable params: 651072 (2.48 MB)
Non-trainable params: 960 (3.75 KB)
✓ CNN Model built successfully!

Loading dataset...
✓ Loaded 100 images
✓ Image shape: (70, 280, 1)

Testing feature extraction with first image...


✓ Input shape: (1, 70, 280, 1)
✓ Output features shape: (1, 35, 128)
✓ Equation: x+5=8
✓ Model saved to: models/cnn_extractor.h5
CNN Feature Extractor is ready!
Next step: Build the RNN Sequence Model

RNN Sequence Model

import tensorflow as tf
from tensorflow import keras
from [Link] import layers
import numpy as np
import json
import os

class RNNSequenceModel:
def __init__(self, num_chars, max_seq_length=15):
self.num_chars = num_chars
self.max_seq_length = max_seq_length
[Link] = None
def build_model(self, feature_dim=128, time_steps=35):
inputs = [Link](shape=(time_steps, feature_dim), name='feature_input')

x = [Link]([Link](128, return_sequences=True))(inputs) x =
[Link](0.3)(x)

x = [Link]([Link](64, return_sequences=True))(x) x =
[Link](0.3)(x)
x = [Link]([Link](64, activation='relu'))(x)
outputs = [Link]([Link](self.num_chars + 1,
activation='softmax'))(x)
[Link] = [Link](inputs=inputs, outputs=outputs,
name='RNN_Sequence_Model')

print("✓ RNN Model built successfully!")


[Link]()

return [Link]
def ctc_loss(self, y_true, y_pred):
"""CTC (Connectionist Temporal Classification) loss function""" #
This will be used during training
batch_len = [Link]([Link](y_true)[0], dtype="int64")
input_length = [Link]([Link](y_pred)[1], dtype="int64")
label_length = [Link]([Link](y_true)[1], dtype="int64")

input_length = input_length * [Link](shape=(batch_len,), dtype="int64")


label_length = label_length * [Link](shape=(batch_len,), dtype="int64")

loss = [Link].ctc_batch_cost(y_true, y_pred, input_length, label_length)


return loss

def decode_predictions(self, predictions, char_mapping):


pred_indices = [Link](predictions, axis=-1
decoded_text = []

for sequence in pred_indices:


text = []
last_char = -1

for char_idx in sequence:


if char_idx == self.num_chars:
continue
if char_idx != last_char:
if char_idx < len(char_mapping):
[Link](char_mapping[char_idx])
last_char = char_idx
decoded_text.append(''.join(text))

return decoded_text
def save_model(self, filepath='models/rnn_sequence.h5')
[Link]([Link](filepath), exist_ok=True)
[Link](filepath)
print(f"✓ Model saved to: {filepath}")

def load_model(self, filepath='models/rnn_sequence.h5'):


[Link] = [Link].load_model(filepath)
print(f"✓ Model loaded from: {filepath}")
return [Link]

def prepare_labels(labels_list, max_length=15, num_chars=17):


padded_labels = []

for labels in labels_list:


if len(labels) < max_length:
padded = labels + [num_chars] * (max_length - len(labels))
else:
padded = labels[:max_length]
padded_labels.append(padded)

return [Link](padded_labels)

def test_rnn()
print("="*60)
print("Testing RNN Sequence Model")
print("="*60)

with open('math_dataset/char_mapping.json', 'r') as f:


char_data = [Link](f)

char_to_idx = char_data['char_to_idx']
idx_to_char = {int(k): v for k, v in char_data['idx_to_char'].items()}
num_chars = len(char_to_idx)

print(f"Number of characters: {num_chars}")


print(f"Characters: {list(char_to_idx.keys())}")

rnn = RNNSequenceModel(num_chars=num_chars, max_seq_length=15)

model = rnn.build_model(feature_dim=128, time_steps=35)


print("\nTesting sequence prediction...")
dummy_features = [Link](1, 35, 128).astype(np.float32)
predictions = [Link](dummy_features, verbose=0)
print(f"✓ Input features shape: {dummy_features.shape}")
print(f"✓ Output predictions shape: {[Link]}")

decoded = rnn.decode_predictions(predictions, idx_to_char)


print(f"✓ Decoded text (random): {decoded[0]}")
rnn.save_model()

print("\n" + "="*60)
print("RNN Sequence Model is ready!")
print("Next step: Combine CNN + RNN for complete pipeline")
print("="*60)
return rnn, idx_to_char

if __name__ == '__main__':
test_rnn()

Output
Testing RNN Sequence Model
Number of characters: 17
Characters: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'x', '+', '-', '=', '(', ')', ' ']

Model: "RNN_Sequence_Model"

Layer (type) Output Shape Param # feature_input


(InputLayer) [(None, 35, 128)] 0
bidirectional (Bidirection (None, 35, 256) 263168 al)

dropout (Dropout) (None, 35, 256) 0

bidirectional_1 (Bidirecti (None, 35, 128) 164352 onal)

dropout_1 (Dropout) (None, 35, 128) 0

time_distributed (TimeDist (None, 35, 64) 8256 ributed)

time_distributed_1 (TimeDi (None, 35, 18) 1170 stributed)


Total params: 436946 (1.67 MB)
Trainable params: 436946 (1.67 MB)
Non-trainable params: 0 (0.00 Byte)

✓ RNN Model built successfully!

Testing sequence prediction...


✓ Input features shape: (1, 35, 128)
✓ Output predictions shape: (1, 35, 18)
✓ Decoded text (random): 3-x+
✓ Model saved to: models/rnn_sequence.h5
RNN Sequence Model is ready!
Test_train Model
import tensorflow as tf
from tensorflow import keras
import numpy as np
import cv2
import json
import os
from sympy import symbols, Eq, solve
from [Link].sympy_parser import parse_expr

class SimpleOCRTester:
def __init__(self):
self.img_width = 280
self.img_height = 70
self.max_text_len = 15

# Load character mapping


with open('math_dataset/char_mapping.json', 'r') as f:
char_data = [Link](f)

self.char_to_idx = char_data['char_to_idx']
self.idx_to_char = {int(k): v for k, v in char_data['idx_to_char'].items()}
self.num_chars = len(self.char_to_idx)

# Load model
print("Loading model...")
[Link] = [Link].load_model('models/simple_ocr_final.h5')
print("✓ Model loaded!")

def preprocess_image(self, image_path):


"""Preprocess image"""
img = [Link](image_path, cv2.IMREAD_GRAYSCALE)
img = [Link](img, (self.img_width, self.img_height))
img = [Link](np.float32) / 255.0
img = np.expand_dims(img, axis=-1)
img = np.expand_dims(img, axis=0)
return img

def decode_prediction(self, predictions):


"""Decode model predictions"""
text = []
for pred in predictions:
char_idx = [Link](pred[0])
if char_idx > 0 and char_idx in self.idx_to_char:
[Link](self.idx_to_char[char_idx])
return ''.join(text).strip()
def recognize(self, image_path):
img = self.preprocess_image(image_path)
predictions = [Link](img, verbose=0)
equation = self.decode_prediction(predictions)
return equation

def solve_equation(self, equation_text):


try:
equation_text = equation_text.strip()
x = symbols('x')

if '=' in equation_text:
left, right = equation_text.split('=')

# Add explicit multiplication: 7x -> 7*x, 2x -> 2*x


import re
left = [Link](r'(\d)([x])', r'\1*\2', left)
right = [Link](r'(\d)([x])', r'\1*\2', right)

left_expr = parse_expr([Link](' ', ''))


right_expr = parse_expr([Link](' ', ''))
equation = Eq(left_expr, right_expr)
solution = solve(equation, x)

if solution:
return solution[0]
else:
return "No solution"
else:
return "No '=' found"
except Exception as e:
return f"Error: {str(e)}"

def test_samples(self, num_samples=20):


print("\n" + "="*70)
print("TESTING SIMPLE OCR MODEL")
print("="*70)

with open('math_dataset/dataset_info.json', 'r') as f:


dataset_info = [Link](f)

correct = 0

for i, item in enumerate(dataset_info[:num_samples]): img_path =


[Link]('math_dataset/images', item['image']) true_eq =
item['equation']
true_sol = item['solution']
pred_eq = [Link](img_path)
pred_sol = self.solve_equation(pred_eq)

is_correct = (pred_eq.replace(' ', '') == true_eq.replace(' ', '')) if


is_correct:
correct += 1

print(f"\n[{i+1}/{num_samples}]")
print(f" True: {true_eq} → x = {true_sol}")
print(f" Predicted: {pred_eq} → x = {pred_sol}")
print(f" {'✓ CORRECT' if is_correct else '✗ WRONG'}")

accuracy = (correct / num_samples) * 100

print("\n" + "="*70)
print(f"ACCURACY: {correct}/{num_samples} = {accuracy:.1f}%")
print("="*70)

return accuracy

def main():
print("\n" + "="*70)
print("SIMPLE OCR MODEL TESTING")
print("="*70)

tester = SimpleOCRTester()
accuracy = tester.test_samples(num_samples=20)

if accuracy > 80:


print("\n Excellent performance!")
elif accuracy > 60:
print("\n✓ Good performance!")
elif accuracy > 40:
print("\n⚠ Moderate performance")
else:
print("\n⚠ Needs improvement")

if __name__ == '__main__':
main()

Output
SIMPLE OCR MODEL TESTING
Loading model...
✓ Model loaded!
TESTING SIMPLE OCR MODEL

[1/20]
True: x+5=8 → x = 3
Predicted: x+5=8 → x = 3
✓ CORRECT

[2/20]
True: 7x+2=16 → x = 2
Predicted: 7x+2=16 → x = 2
✓ CORRECT

[3/20]
True: 2x+6=10 → x = 2
Predicted: 2x+6=10 → x = 2
✓ CORRECT

Conclusion:
Deep learning offers a promising and innovative approach to solving mathematical
equations by learning patterns from data rather than relying solely on rule-based
computation. While current models can effectively handle simple and structured
equations, further advancements in model design and data representation are
needed for handling complex symbolic mathematics. Combining deep learning
with traditional symbolic computation could lead to more powerful hybrid systems
for mathematical problem-solving in the future.

You might also like