SkillForge45 commited on
Commit
7cd3038
verified
1 Parent(s): e8d8334

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +71 -0
main.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from tensorflow.keras.models import Sequential
5
+ from tensorflow.keras.layers import Dense, Flatten
6
+ from tensorflow.keras.utils import to_categorical
7
+
8
+ # Constants
9
+ CHAR_SET = '0123456789+-=* /'
10
+ NUM_CLASSES = len(CHAR_SET)
11
+ MAX_EQUATION_LENGTH = 20 # Adjust based on the longest equation in your dataset
12
+ MAX_RESULT_LENGTH = 10 # Adjust based on the longest result in your dataset
13
+
14
+ def one_hot_encode(s, max_length):
15
+ encoding = np.zeros((max_length, NUM_CLASSES))
16
+ for i, char in enumerate(s[:max_length]):
17
+ if char in CHAR_SET:
18
+ char_index = CHAR_SET.index(char)
19
+ encoding[i, char_index] = 1
20
+ return encoding
21
+
22
+ def read_dataset(directory):
23
+ data = []
24
+ labels = []
25
+
26
+ for filename in os.listdir(directory):
27
+ if filename.endswith('.txt'):
28
+ with open(os.path.join(directory, filename), 'r') as file:
29
+ for line in file:
30
+ line = line.strip()
31
+ if '=' in line:
32
+ equation, result = line.split('=')
33
+ equation = equation.strip()
34
+ result = result.strip()
35
+ data.append(one_hot_encode(equation, MAX_EQUATION_LENGTH))
36
+ labels.append(one_hot_encode(result, MAX_RESULT_LENGTH))
37
+
38
+ return np.array(data), np.array(labels)
39
+
40
+ # Read dataset
41
+ data, labels = read_dataset('.math_train')
42
+
43
+ # Reshape labels for categorical crossentropy
44
+ labels = labels.reshape((labels.shape[0], -1, NUM_CLASSES))
45
+
46
+ # Build the model
47
+ model = Sequential([
48
+ Flatten(input_shape=(MAX_EQUATION_LENGTH, NUM_CLASSES)),
49
+ Dense(128, activation='relu'),
50
+ Dense(64, activation='relu'),
51
+ Dense(MAX_RESULT_LENGTH * NUM_CLASSES, activation='softmax')
52
+ ])
53
+
54
+ model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
55
+
56
+ # Train the model
57
+ model.fit(data, labels.reshape((-1, MAX_RESULT_LENGTH * NUM_CLASSES)), epochs=50, batch_size=32)
58
+
59
+ # Function to solve an equation
60
+ def solve_equation(model, equation):
61
+ encoded_equation = one_hot_encode(equation, MAX_EQUATION_LENGTH)
62
+ input_tensor = np.expand_dims(encoded_equation, axis=0)
63
+ prediction = model.predict(input_tensor)
64
+ predicted_indices = np.argmax(prediction.reshape((MAX_RESULT_LENGTH, NUM_CLASSES)), axis=-1)
65
+ predicted_chars = ''.join(CHAR_SET[i] for i in predicted_indices if i < len(CHAR_SET))
66
+ return predicted_chars.strip()
67
+
68
+
69
+ equation = "1 + 1"
70
+ result = solve_equation(model, equation)
71
+ print(f"The result of '{equation}' is '{result}'")