-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBatsy.py
More file actions
270 lines (210 loc) · 8.22 KB
/
TestBatsy.py
File metadata and controls
270 lines (210 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import os
from transformers import AutoTokenizer
from safe_rlhf.models import AutoModelForScore
model = AutoModelForScore.from_pretrained('PKU-Alignment/beaver-7b-v1.0-reward', device_map='auto')
tokenizer = AutoTokenizer.from_pretrained('PKU-Alignment/beaver-7b-v1.0-reward', use_fast=False)
input = 'BEGINNING OF CONVERSATION: USER: hello ASSISTANT:Hello! How can I help you today?'
input_ids = tokenizer(input, return_tensors='pt')
output = model(**input_ids)
print(output)
# ScoreModelOutput(
# scores=tensor([[[-19.6476],
# [-20.2238],
# [-21.4228],
# [-19.2506],
# [-20.2728],
# [-23.8799],
# [-22.6898],
# [-21.5825],
# [-21.0855],
# [-20.2068],
# [-23.8296],
# [-21.4940],
# [-21.9484],
# [-13.1220],
# [ -6.4499],
# [ -8.1982],
# [ -7.2492],
# [ -9.3377],
# [-13.5010],
# [-10.4932],
# [ -9.7837],
# [ -6.4540],
# [ -6.0084],
# [ -5.8093],
# [ -6.6134],
# [ -5.8995],
# [ -9.1505],
# [-11.3254]]], grad_fn=<ToCopyBackward0>),
# end_scores=tensor([[-11.3254]], grad_fn=<ToCopyBackward0>)
# )
import requests
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import torch
import tensorflow as tf
import onnx
import discord
from discord.ext import commands
import gym
# Load PyTorch and TensorFlow models
pytorch_model = torch.load('your_pytorch_model.pth')
tf_model = tf.keras.models.load_model('your_tensorflow_model.h5')
# Convert to ONNX
onnx_model = onnx.export(pytorch_model, ...)
const Discord=require('discord.py');
const client = new Discord.Client();
client.login();
#Replace 'YOUR_BOT_TOKEN' with the token you copied earlier.
intents = discord.Intents.default()
intents.typing = max_features
intents.presences = max_features
client = discord.Client(intents=intents)
# Define the bot's prefix and command
BOT_PREFIX = "!"
COMMAND = "ask"
@client.event
async def on_ready():
print(f"We have logged in as {client.user}")
@client.event
async def on_message(message):
if message.author == client.user:
return
def chat_gpt(prompt):
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY" # Replace with your actual API key
}
data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt}]
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
if __name__ == "__main__":
prompt = "Translate the following English text to French: 'Hello, how are you?'"
response = chat_gpt(prompt)
print(response)
def objective_function(x):
return -(x ** 2) # Negative because we want to find the maximum
def hill_climbing(starting_point, step_size, num_iterations):
current_point = starting_point
for _ in range(num_iterations):
current_value = objective_function(current_point)
# Evaluate neighboring points
left_neighbor = current_point - step_size
right_neighbor = current_point + step_size
left_value = objective_function(left_neighbor)
right_value = objective_function(right_neighbor)
# Move to the neighbor with the higher value
if left_value > current_value:
current_point = left_neighbor
elif right_value > current_value:
current_point = right_neighbor
return current_point, objective_function(current_point)
if __name__ == "__main__":
starting_point = 2
step_size = 0.1
num_iterations = 100
final_point, max_value = hill_climbing(starting_point, step_size, num_iterations)
print(f"The maximum value is {max_value} at x = {final_point}")
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'Bot is logged in as {client.user}')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
await message.channel.send('Hello!')
client.run('MTE0NjkwNDk2Nzc2NTA1MzQ2MA.GfKac2.HiGYOK0g8ocZBoqNt-kyXGEtstW4OsO7JJZbKg')
python discord_bot.py
# Define the environment (0: Empty cell, 1: Obstacle, 2: Goal)
grid = np.array([[0, 0, 1, 0, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 0, 1],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 2]])
# Initialize Q-table
num_states = grid.size
num_actions = 4 # 4 possible actions: up, down, left, right
q_table = np.zeros((num_states, num_actions))
# Parameters
learning_rate = 0.8
discount_factor = 0.95
exploration_prob = 0.2
episodes = 1000
# Helper function to get next state and reward
def get_next_state(current_state, action):
if action == 0: # Move up
next_state = (max(current_state[0] - 1, 0), current_state[1])
elif action == 1: # Move down
next_state = (min(current_state[0] + 1, grid.shape[0]-1), current_state[1])
elif action == 2: # Move left
next_state = (current_state[0], max(current_state[1] - 1, 0))
elif action == 3: # Move right
next_state = (current_state[0], min(current_state[1] + 1, grid.shape[1]-1))
reward = grid[next_state[0], next_state[1]]
return next_state, reward
# Q-learning algorithm
for _ in range(episodes):
current_state = (0, 0)
while True:
if np.random.uniform(0, 1) < exploration_prob:
action = np.random.randint(num_actions) # Explore
else:
action = np.argmax(q_table[current_state[0]*grid.shape[1] + current_state[1]]) # Exploit
next_state, reward = get_next_state(current_state, action)
q_table[current_state[0]*grid.shape[1] + current_state[1], action] += learning_rate * (reward +
discount_factor * np.max(q_table[next_state[0]*grid.shape[1] + next_state[1]]) -
q_table[current_state[0]*grid.shape[1] + current_state[1], action])
if reward == 2: # Reached the goal
break
current_state = next_state
# Now, q_table contains the learned Q-values.
# You can use these values to make optimal decisions in the environment.
class SelfUpdatingModel:
def __init__(self):
self.weights = [0.5, 0.3, -0.2] # Initial weights (for simplicity)
def predict(self, features):
return sum(w * f for w, f in zip(self.weights, features))
def update_weights(self, features, target, learning_rate):
prediction = self.predict(features)
error = target - prediction
self.weights = [w + learning_rate * error * f for w, f in zip(self.weights, features)]
# Example usage
model = SelfUpdatingModel()
# Simulated data (features, target)
data = [([1, 2, 3], 7), ([2, 3, 4], 12), ([3, 4, 5], 17)]
for features, target in data:
prediction = model.predict(features)
print(f"Predicted: {prediction}, Actual: {target}")
model.update_weights(features, target, learning_rate=0.1)
print(f"Updated weights: {model.weights}")
# Now the model has been updated based on the data
# Generate some sample data
np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
# Train-test split (for evaluation)
X_train, X_test = X[:80], X[80:]
y_train, y_test = y[:80], y[80:]
# Train the linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict on the test set
y_pred = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
# Visualize the results
plt.scatter(X_test, y_test, color='blue')
plt.plot(X_test, y_pred, color='red', linewidth=2)
plt.show()