Skip to content

Commit 35e27f3

Browse files
Fixed the actual neural_net browser file that neede fixing.
1 parent d99f5d1 commit 35e27f3

2 files changed

Lines changed: 2 additions & 145 deletions

File tree

src/game_2048/neural_net.js

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,4 @@
11

2-
// // Complete ONNX setup that works in both Node.js and browser
3-
// let ort;
4-
// let session;
5-
// const MOVE_DIRECTIONS = ['up', 'down', 'left', 'right'];
6-
7-
8-
9-
// function getModelPath() {
10-
// if (typeof window !== 'undefined') {
11-
// // Base-aware path
12-
// return import.meta.env.BASE_URL + 'Onnx_models/model_2048_for_js.onnx';
13-
// } else {
14-
// // Node.js - can use relative path
15-
// return 'public/Onnx_models/model_2048_for_js.onnx';
16-
// }
17-
// }
18-
19-
20-
// async function initializeOrt() {
21-
// if (!ort) {
22-
// if (typeof window !== 'undefined') {
23-
// // Browser
24-
// const ortWeb = await import('onnxruntime-web');
25-
26-
// // ortWeb.env.wasm.wasmPaths = 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.16.3/dist/';
27-
// ortWeb.env.wasm.wasmPaths = import.meta.env.BASE_URL + 'assets/';
28-
// ortWeb.env.wasm.numThreads = 1;
29-
// ortWeb.env.logLevel = 'warning';
30-
31-
// try {
32-
// await ortWeb.env.wasm.init();
33-
// } catch (err) {
34-
// console.warn('WASM init failed, fallback to CPU:', err);
35-
// }
36-
37-
// ort = ortWeb;
38-
// } else {
39-
// // Node
40-
// const ortNode = await import('onnxruntime-node');
41-
// ort = ortNode;
42-
// }
43-
// }
44-
// console.log('ONNX Runtime initialized:', ort);
45-
// console.log("using " + (typeof window !== 'undefined' ? "onnxruntime-web" : "onnxruntime-node"));
46-
// return ort;
47-
// }
48-
49-
// async function loadModel(modelPath = getModelPath()) {
50-
// await initializeOrt();
51-
52-
// try {
53-
// const options = {};
54-
55-
// // For browser, specify execution providers explicitly
56-
// if (typeof window !== 'undefined') {
57-
// options.executionProviders = ['cpu']; // Safe fallback
58-
// }
59-
60-
// session = await ort.InferenceSession.create(modelPath, options);
61-
// console.log('Model loaded successfully');
62-
// } catch (error) {
63-
// console.error('Failed to load model:', error);
64-
65-
// // Try with different execution providers as fallback
66-
// if (typeof window !== 'undefined') {
67-
// try {
68-
// console.log('Trying with WASM execution provider...');
69-
// const wasmOptions = { executionProviders: ['wasm'] };
70-
// session = await ort.InferenceSession.create(modelPath, wasmOptions);
71-
// console.log('Model loaded with WASM provider');
72-
// } catch (wasmError) {
73-
// console.error('WASM fallback also failed:', wasmError);
74-
// throw error;
75-
// }
76-
// } else {
77-
// throw error;
78-
// }
79-
// }
80-
// }
81-
82-
// export async function NeuralNet(gameboard) {
83-
// console.log("in NeuralNet function");
84-
// const preProcessBoard = (board) => {
85-
// return board.flat().map(x => Math.log2(x === 0 ? 1 : x) / 16); //normalize by max tile (65536)
86-
// };
87-
88-
// // Ensure model is loaded
89-
// if (!session) {
90-
// await loadModel();
91-
// }
92-
93-
// // Ensure ort is initialized
94-
// if (!ort) {
95-
// await initializeOrt();
96-
// }
97-
98-
// try {
99-
// // Create input tensor
100-
// const processedBoard = preProcessBoard(gameboard);
101-
// console.log("Processed Board for NN:", processedBoard);
102-
103-
// const inputTensor = new ort.Tensor('float32',
104-
// Float32Array.from(processedBoard),
105-
// [1, processedBoard.length]
106-
// );
107-
108-
// const feeds = { input: inputTensor };
109-
110-
// // Run inference
111-
// const results = await session.run(feeds);
112-
// const output = results.output.data; // Array of scores for each move
113-
114-
// // Find index of max score
115-
// let maxIndex = 0;
116-
// let maxScore = output[0];
117-
118-
// for (let i = 1; i < output.length; i++) {
119-
// if (output[i] > maxScore) {
120-
// maxIndex = i;
121-
// maxScore = output[i];
122-
// }
123-
// }
124-
125-
// return {
126-
// direction: MOVE_DIRECTIONS[maxIndex],
127-
// confidence: maxScore,
128-
// allScores: Array.from(output)
129-
// };
130-
131-
// } catch (error) {
132-
// console.error('Error during neural network inference:', error);
133-
// throw error;
134-
// }
135-
// }
136-
137-
// // Optional: Add a function to cleanup resources
138-
// export function cleanup() {
139-
// if (session) {
140-
// session.release();
141-
// session = null;
142-
// }
143-
// }
144-
1452
// Complete ONNX setup that works in both Node.js and browser
1463
let ort;
1474
let session;
@@ -150,7 +7,7 @@ const MOVE_DIRECTIONS = ['up', 'down', 'left', 'right'];
1507
// Base-aware model path
1518
function getModelPath() {
1529
if (typeof window !== 'undefined') {
153-
return import.meta.env.BASE_URL + 'Onnx_models/model_2048_for_js.onnx';
10+
return import.meta.env.BASE_URL + 'react_playground/Onnx_models/model_2048_for_js.onnx';
15411
} else {
15512
return 'public/Onnx_models/model_2048_for_js.onnx';
15613
}

src/game_2048/neural_net_browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const MOVE_DIRECTIONS = ['UP', 'DOWN', 'LEFT', 'RIGHT'];
66

77
function getModelPath() {
88
// Browser: model must be in public directory
9-
return '/Onnx_models/model_2048_for_js.onnx';
9+
return import.meta.env.BASE_URL + 'Onnx_models/model_2048_for_js.onnx';
1010
}
1111

1212
let session;

0 commit comments

Comments
 (0)