veda-programming / train.py
vedaco's picture
Update train.py
ed6fa70 verified
raw
history blame
8.62 kB
"""Training - MODIFIED with conversation data"""
import tensorflow as tf
from tensorflow import keras
import numpy as np
import os
import json
from model import VedaProgrammingLLM
from tokenizer import VedaTokenizer
from config import MODEL_DIR, VOCAB_SIZE, MAX_LENGTH, BATCH_SIZE
# ADDED: Conversation training data
TRAINING_DATA = '''
<USER> Hello!
<ASSISTANT> Hello! Welcome to Veda Programming Assistant! I'm here to help you with coding, explain concepts, or answer any programming questions. What would you like to learn today?
<USER> What is Python?
<ASSISTANT> Python is a high-level, interpreted programming language created by Guido van Rossum in 1991. It's known for its simple, readable syntax that emphasizes code readability.
Key features:
- Easy to learn and read
- Dynamic typing
- Large standard library
- Great for beginners and experts alike
Python is used for web development, data science, AI, automation, and much more!
<USER> How do I print hello world?
<ASSISTANT> To print "Hello, World!" in Python, use the print() function:
<CODE>
print("Hello, World!")
<ENDCODE>
When you run this, it displays: Hello, World!
The print() function outputs text to the console. You can print strings, numbers, and variables!
<USER> Write a function to add two numbers
<ASSISTANT> Here's a simple function to add two numbers:
<CODE>
def add(a, b):
return a + b
# Example usage
result = add(5, 3)
print(result) # Output: 8
<ENDCODE>
This function takes two parameters and returns their sum.
<USER> Explain what a function is
<ASSISTANT> A function is a reusable block of code that performs a specific task. Functions help organize code, avoid repetition, and make programs easier to understand.
Here's the basic structure:
<CODE>
def function_name(parameters):
# Code block
return result
<ENDCODE>
Parts of a function:
- def: keyword to define a function
- function_name: what you call your function
- parameters: input values (optional)
- return: sends back a result (optional)
Example:
<CODE>
def greet(name):
message = f"Hello, {name}!"
return message
print(greet("Alice")) # Output: Hello, Alice!
<ENDCODE>
<USER> What is a list?
<ASSISTANT> A list is an ordered, mutable collection in Python that can hold items of any type.
Creating lists:
<CODE>
# Empty list
my_list = []
# List with items
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", 3.14, True]
<ENDCODE>
Common operations:
<CODE>
fruits = ["apple", "banana"]
# Add items
fruits.append("cherry") # Add to end
fruits.insert(0, "apricot") # Insert at position
# Access items
print(fruits[0]) # First item
print(fruits[-1]) # Last item
# Remove items
fruits.remove("banana") # Remove by value
fruits.pop() # Remove last item
# Length
print(len(fruits))
<ENDCODE>
<USER> Write a fibonacci function
<ASSISTANT> Here's a Fibonacci function with two approaches:
Recursive approach:
<CODE>
def fibonacci(n):
if n <= 0:
return 0
if n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)
# Print first 10 numbers
for i in range(10):
print(fibonacci(i), end=" ")
# Output: 0 1 1 2 3 5 8 13 21 34
<ENDCODE>
Iterative approach (more efficient):
<CODE>
def fibonacci_iterative(n):
if n <= 0:
return 0
if n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
<ENDCODE>
The Fibonacci sequence: each number is the sum of the two before it!
<USER> What is recursion?
<ASSISTANT> Recursion is when a function calls itself to solve a problem. It breaks complex problems into smaller, similar sub-problems.
Key components:
1. Base case - stops the recursion
2. Recursive case - function calls itself
Example - countdown:
<CODE>
def countdown(n):
if n <= 0: # Base case
print("Done!")
return
print(n)
countdown(n - 1) # Recursive call
countdown(5)
# Output: 5, 4, 3, 2, 1, Done!
<ENDCODE>
Another example - factorial:
<CODE>
def factorial(n):
if n <= 1: # Base case
return 1
return n * factorial(n - 1) # Recursive call
print(factorial(5)) # Output: 120
<ENDCODE>
Warning: Always have a base case to avoid infinite recursion!
<USER> Write bubble sort
<ASSISTANT> Here's the Bubble Sort algorithm:
<CODE>
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break
return arr
# Example
numbers = [64, 34, 25, 12, 22, 11, 90]
print("Original:", numbers)
print("Sorted:", bubble_sort(numbers.copy()))
<ENDCODE>
Output:
Original: [64, 34, 25, 12, 22, 11, 90]
Sorted: [11, 12, 22, 25, 34, 64, 90]
How it works: repeatedly compares adjacent elements and swaps them if in wrong order. Largest elements "bubble up" to the end.
Time complexity: O(n²)
<USER> Write binary search
<ASSISTANT> Here's the Binary Search algorithm:
<CODE>
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1 # Not found
# Example
numbers = [1, 3, 5, 7, 9, 11, 13, 15]
print(binary_search(numbers, 7)) # Output: 3
print(binary_search(numbers, 6)) # Output: -1
<ENDCODE>
Important: The array must be sorted!
Time complexity: O(log n) - much faster than linear search!
<USER> What is a class?
<ASSISTANT> A class is a blueprint for creating objects. It bundles data (attributes) and functions (methods) together.
Basic class structure:
<CODE>
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return f"{self.name} says Woof!"
def get_info(self):
return f"{self.name} is {self.age} years old"
# Create objects
my_dog = Dog("Buddy", 3)
print(my_dog.bark()) # Buddy says Woof!
print(my_dog.get_info()) # Buddy is 3 years old
<ENDCODE>
Key concepts:
- __init__: constructor, runs when object is created
- self: refers to the current object
- Attributes: variables inside the class
- Methods: functions inside the class
<USER> How do I read a file?
<ASSISTANT> Here's how to read files in Python:
Basic file reading:
<CODE>
# Read entire file
with open("filename.txt", "r") as file:
content = file.read()
print(content)
# Read line by line
with open("filename.txt", "r") as file:
for line in file:
print(line.strip())
# Read all lines into list
with open("filename.txt", "r") as file:
lines = file.readlines()
<ENDCODE>
With error handling:
<CODE>
def read_file_safely(filename):
try:
with open(filename, "r") as file:
return file.read()
except FileNotFoundError:
print(f"File '{filename}' not found")
return None
content = read_file_safely("myfile.txt")
<ENDCODE>
The 'with' statement automatically closes the file when done - always recommended!
<USER> How do I handle errors?
<ASSISTANT> Python uses try-except blocks for error handling:
Basic try-except:
<CODE>
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
<ENDCODE>
Multiple exceptions:
<CODE>
try:
number = int(input("Enter number: "))
result = 10 / number
except ValueError:
print("That's not a valid number!")
except ZeroDivisionError:
print("Cannot divide by zero!")
except Exception as e:
print(f"Error: {e}")
<ENDCODE>
Complete structure:
<CODE>
try:
result = risky_operation()
except SomeError:
print("Handle error")
else:
print("Success!") # Runs if no error
finally:
print("Always runs") # Cleanup code
<ENDCODE>
Common exceptions:
- ValueError: wrong value
- TypeError: wrong type
- IndexError: index out of range
- KeyError: dictionary key not found
- FileNotFoundError: file doesn't exist
<USER> Thanks!
<ASSISTANT> You're welcome! I'm glad I could help. Remember:
- Practice coding every day
- Build small projects
- Don't fear errors - they help you learn
- Ask questions whenever stuck
Feel free to ask me anything anytime! Happy coding! 🐍
<USER> debug this: def add(a b): return a + b
<ASSISTANT> I found the issue! You're missing a comma between parameters.
Your code:
```python
def add(a b): return a + b # ❌ Missing comma