Git Product home page Git Product logo

ai's Introduction

ai's People

Contributors

ccckmit avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

ai's Issues

習題 1 : 請寫一個排課系統 (如果做不出來,可以找一個 NP-Complete 問題取代也行)

說明:

有兩間教室 A, B , 有五個老師 甲乙丙丁戊 , 有很多課程參考下列的資料表 (上面有課程一週上幾小時,是哪個老師上的)

要求:可以用,也可以不用爬山演算法

參考程式

  1. 物件導向版 : 有排課系統與爬山演算法框架
  2. 非物件導向版 :沒排課系統,有爬山演算法框架

若做不出來,可以選一個 NP-Complete 問題求解(不需要最佳解,只要是不錯的解就行)

建議可以試試旅行推銷員問題

課程資訊

courses = [
{'teacher': '  ', 'name':'  ', 'hours': -1}, // 那一節沒上課
{'teacher': '甲', 'name':'機率', 'hours': 2},
{'teacher': '甲', 'name':'線代', 'hours': 3},
{'teacher': '甲', 'name':'離散', 'hours': 3},
{'teacher': '乙', 'name':'視窗', 'hours': 3},
{'teacher': '乙', 'name':'科學', 'hours': 3},
{'teacher': '乙', 'name':'系統', 'hours': 3},
{'teacher': '乙', 'name':'計概', 'hours': 3},
{'teacher': '丙', 'name':'軟工', 'hours': 3},
{'teacher': '丙', 'name':'行動', 'hours': 3},
{'teacher': '丙', 'name':'網路', 'hours': 3},
{'teacher': '丁', 'name':'媒體', 'hours': 3},
{'teacher': '丁', 'name':'工數', 'hours': 3},
{'teacher': '丁', 'name':'動畫', 'hours': 3},
{'teacher': '丁', 'name':'電子', 'hours': 4},
{'teacher': '丁', 'name':'嵌入', 'hours': 3},
{'teacher': '戊', 'name':'網站', 'hours': 3},
{'teacher': '戊', 'name':'網頁', 'hours': 3},
{'teacher': '戊', 'name':'演算', 'hours': 3},
{'teacher': '戊', 'name':'結構', 'hours': 3},
{'teacher': '戊', 'name':'智慧', 'hours': 3}
]

teachers = ['甲', '乙', '丙', '丁', '戊']

rooms = ['A', 'B']

slots = [
'A11', 'A12', 'A13', 'A14', 'A15', 'A16', 'A17',
'A21', 'A22', 'A23', 'A24', 'A25', 'A26', 'A27',
'A31', 'A32', 'A33', 'A34', 'A35', 'A36', 'A37',
'A41', 'A42', 'A43', 'A44', 'A45', 'A46', 'A47',
'A51', 'A52', 'A53', 'A54', 'A55', 'A56', 'A57',
'B11', 'B12', 'B13', 'B14', 'B15', 'B16', 'B17',
'B21', 'B22', 'B23', 'B24', 'B25', 'B26', 'B27',
'B31', 'B32', 'B33', 'B34', 'B35', 'B36', 'B37',
'B41', 'B42', 'B43', 'B44', 'B45', 'B46', 'B47',
'B51', 'B52', 'B53', 'B54', 'B55', 'B56', 'B57',
]

期末作業:請將你的平時作業集中交上來,並交代哪些有做完,哪些沒做 (2024/6/22 日週六 24:00 前繳交完畢)

最後兩週可以來現場繳交,這樣老師會比較清楚你的作業,不需要寫太多說明,也比較不需要猜測。(如果老師需要猜測,那就會用最保守的猜測方法,意思是成績會比較差)

規定

  1. 請紀錄每一題習題的完成方法(例如:全部原創,有參考維基百科,但沒有剪貼)
    • 是自己寫的(原創)/參考誰的/修改誰的/參考什麼網路資源/直接複製誰的(有看懂/沒看懂/有改過)

特別提醒

  1. 『每一題』都要標示:原創,複製或修改(修改來源)
  2. 如果全部都是原創,直接在最前面寫『全部原創』
  3. 如果全部都是複製沒修改,直接在最前面寫『全部複製 xxx 的沒修改』
  4. 如果大部分是原創,少部分修改,就在最前面寫『除了 xxx 題以外,都是原創』
    • xxx 題參考 yyy 網址,修改了 zzz 內容

習題7: 請自己定義一個神經網路模型,並在 MNIST 資料集上訓練並跑出正確率

參考

  1. 深度學習/01-MNIST
  2. https://cs.stanford.edu/people/karpathy/convnetjs/demo/mnist.html

卷積層的 JavaScript 實作

https://github.com/karpathy/convnetjs/blob/master/src/convnet_layers_dotproducts.js

  var ConvLayer = function(opt) {
    var opt = opt || {};

    // required
    this.out_depth = opt.filters;
    this.sx = opt.sx; // filter size. Should be odd if possible, it's cleaner.
    this.in_depth = opt.in_depth;
    this.in_sx = opt.in_sx;
    this.in_sy = opt.in_sy;
    
    // optional
    this.sy = typeof opt.sy !== 'undefined' ? opt.sy : this.sx;
    this.stride = typeof opt.stride !== 'undefined' ? opt.stride : 1; // stride at which we apply filters to input volume
    this.pad = typeof opt.pad !== 'undefined' ? opt.pad : 0; // amount of 0 padding to add around borders of input volume
    this.l1_decay_mul = typeof opt.l1_decay_mul !== 'undefined' ? opt.l1_decay_mul : 0.0;
    this.l2_decay_mul = typeof opt.l2_decay_mul !== 'undefined' ? opt.l2_decay_mul : 1.0;

    // computed
    // note we are doing floor, so if the strided convolution of the filter doesnt fit into the input
    // volume exactly, the output volume will be trimmed and not contain the (incomplete) computed
    // final application.
    this.out_sx = Math.floor((this.in_sx + this.pad * 2 - this.sx) / this.stride + 1);
    this.out_sy = Math.floor((this.in_sy + this.pad * 2 - this.sy) / this.stride + 1);
    this.layer_type = 'conv';

    // initializations
    var bias = typeof opt.bias_pref !== 'undefined' ? opt.bias_pref : 0.0;
    this.filters = [];
    // 注意下列這行,決定了卷積層的參數數量
    for(var i=0;i<this.out_depth;i++) { this.filters.push(new Vol(this.sx, this.sy, this.in_depth)); }
    this.biases = new Vol(1, 1, this.out_depth, bias);
  }
  ConvLayer.prototype = {
    forward: function(V, is_training) {
      // optimized code by @mdda that achieves 2x speedup over previous version

      this.in_act = V;
      var A = new Vol(this.out_sx |0, this.out_sy |0, this.out_depth |0, 0.0);
      
      var V_sx = V.sx |0;
      var V_sy = V.sy |0;
      var xy_stride = this.stride |0;

      for(var d=0;d<this.out_depth;d++) {
        var f = this.filters[d];
        var x = -this.pad |0;
        var y = -this.pad |0;
        for(var ay=0; ay<this.out_sy; y+=xy_stride,ay++) {  // xy_stride
          x = -this.pad |0;
          for(var ax=0; ax<this.out_sx; x+=xy_stride,ax++) {  // xy_stride
            // 遮罩累加迴圈開始
            // convolve centered at this particular location
            var a = 0.0;
            for(var fy=0;fy<f.sy;fy++) {
              var oy = y+fy; // coordinates in the original input array coordinates
              for(var fx=0;fx<f.sx;fx++) {
                var ox = x+fx;
                if(oy>=0 && oy<V_sy && ox>=0 && ox<V_sx) {
                  for(var fd=0;fd<f.depth;fd++) {
                    // avoid function call overhead (x2) for efficiency, compromise modularity :(
                    a += f.w[((f.sx * fy)+fx)*f.depth+fd] * V.w[((V_sx * oy)+ox)*V.depth+fd];
                  }
                }
              }
            }
            // 遮罩累加迴圈結束
            a += this.biases.w[d]; // 加上 bias
            A.set(ax, ay, d, a); // 設定進結果格子中
          }
        }
      }
      this.out_act = A;
      return this.out_act;
    },
    backward: function() {

      var V = this.in_act;
      V.dw = global.zeros(V.w.length); // zero out gradient wrt bottom data, we're about to fill it

      var V_sx = V.sx |0;
      var V_sy = V.sy |0;
      var xy_stride = this.stride |0;

      for(var d=0;d<this.out_depth;d++) {
        var f = this.filters[d];
        var x = -this.pad |0;
        var y = -this.pad |0;
        for(var ay=0; ay<this.out_sy; y+=xy_stride,ay++) {  // xy_stride
          x = -this.pad |0;
          for(var ax=0; ax<this.out_sx; x+=xy_stride,ax++) {  // xy_stride

            // convolve centered at this particular location
            var chain_grad = this.out_act.get_grad(ax,ay,d); // gradient from above, from chain rule
            for(var fy=0;fy<f.sy;fy++) {
              var oy = y+fy; // coordinates in the original input array coordinates
              for(var fx=0;fx<f.sx;fx++) {
                var ox = x+fx;
                if(oy>=0 && oy<V_sy && ox>=0 && ox<V_sx) {
                  for(var fd=0;fd<f.depth;fd++) {
                    // avoid function call overhead (x2) for efficiency, compromise modularity :(
                    var ix1 = ((V_sx * oy)+ox)*V.depth+fd;
                    var ix2 = ((f.sx * fy)+fx)*f.depth+fd;
                    f.dw[ix2] += V.w[ix1]*chain_grad;
                    V.dw[ix1] += f.w[ix2]*chain_grad;
                  }
                }
              }
            }
            this.biases.dw[d] += chain_grad;
          }
        }
      }
    },
    getParamsAndGrads: function() {
      var response = [];
      for(var i=0;i<this.out_depth;i++) {
        response.push({params: this.filters[i].w, grads: this.filters[i].dw, l2_decay_mul: this.l2_decay_mul, l1_decay_mul: this.l1_decay_mul});
      }
      response.push({params: this.biases.w, grads: this.biases.dw, l1_decay_mul: 0.0, l2_decay_mul: 0.0});
      return response;
    },
    toJSON: function() {
      var json = {};
      json.sx = this.sx; // filter size in x, y dims
      json.sy = this.sy;
      json.stride = this.stride;
      json.in_depth = this.in_depth;
      json.out_depth = this.out_depth;
      json.out_sx = this.out_sx;
      json.out_sy = this.out_sy;
      json.layer_type = this.layer_type;
      json.l1_decay_mul = this.l1_decay_mul;
      json.l2_decay_mul = this.l2_decay_mul;
      json.pad = this.pad;
      json.filters = [];
      for(var i=0;i<this.filters.length;i++) {
        json.filters.push(this.filters[i].toJSON());
      }
      json.biases = this.biases.toJSON();
      return json;
    },
    fromJSON: function(json) {
      this.out_depth = json.out_depth;
      this.out_sx = json.out_sx;
      this.out_sy = json.out_sy;
      this.layer_type = json.layer_type;
      this.sx = json.sx; // filter size in x, y dims
      this.sy = json.sy;
      this.stride = json.stride;
      this.in_depth = json.in_depth; // depth of input volume
      this.filters = [];
      this.l1_decay_mul = typeof json.l1_decay_mul !== 'undefined' ? json.l1_decay_mul : 1.0;
      this.l2_decay_mul = typeof json.l2_decay_mul !== 'undefined' ? json.l2_decay_mul : 1.0;
      this.pad = typeof json.pad !== 'undefined' ? json.pad : 0;
      for(var i=0;i<json.filters.length;i++) {
        var v = new Vol(0,0,0,0);
        v.fromJSON(json.filters[i]);
        this.filters.push(v);
      }
      this.biases = new Vol(0,0,0,0);
      this.biases.fromJSON(json.biases);
    }
  }

習題8: 請自己設計一個固定的策略(不需要學習)解決 CartPole 問題,讓你的竿子盡量撐得久不會倒下來

建議

  1. 不要用機器學習,神經網路,直接用手寫固定策略來解決
  2. 記得先了解 Observation 與 Action ,再開始寫程式

參考

  1. https://gymnasium.farama.org/environments/classic_control/cart_pole/
  2. cartpole_human_run.py
import gymnasium as gym
env = gym.make("CartPole-v1", render_mode="human") # 若改用這個,會畫圖
# env = gym.make("CartPole-v1", render_mode="rgb_array")
observation, info = env.reset(seed=42)
for _ in range(100):
   env.render()
   action = env.action_space.sample()  # 把這裡改成你的公式,看看能撐多久
   observation, reward, terminated, truncated, info = env.step(action)
   print('observation=', observation)
   if terminated or truncated: # 這裡要加入程式,紀錄你每次撐多久
      observation, info = env.reset()
      print('done')
env.close()

期中作業:請寫一個AI作品 (2024/6/22 日週六 24:00 前繳交完畢)

繳交:除線上繳交之外,最後兩週 請到課堂上展示你的系統

最後兩週可以來現場繳交,這樣老師會比較清楚你的作業,不需要寫太多說明,也比較不需要猜測。(如果老師需要猜測,那就會用最保守的猜測方法,意思是成績會比較差)

建議作品

可以是程式專案,也可以是研究報告

  1. Agent
  2. AI 女友
  3. RAG: Retrieval Augmented Generation
  4. 強化學習打遊戲
  5. 機器學習某個資料集
  6. AI 相關演算法研究

注意:請註明下列事項 (例如:全部原創,有參考維基百科,但沒有剪貼)

  • 是自己寫的(原創)/參考誰的/修改誰的/參考什麼網路資源/直接複製誰的(有看懂/沒看懂/有改過)

特別提醒

  1. 『每一題』都要標示:原創,複製或修改(修改來源)
  2. 如果全部都是原創,直接在最前面寫『全部原創』
  3. 如果全部都是複製沒修改,直接在最前面寫『全部複製 xxx 的沒修改』
  4. 如果大部分是原創,少部分修改,就在最前面寫『除了 xxx 題以外,都是原創』
    • xxx 題參考 yyy 網址,修改了 zzz 內容

習題10: 請自己設計 RAG 或 ReAct 的程式 (可以用 langchain 或 dspy)

參考

  1. 提示工程
  2. https://www.langchain.com/
  3. https://console.groq.com/docs/quickstart
  4. 02b-LLM提示工程
  5. https://github.com/stanfordnlp/dspy
  6. RAG 可參考 -- https://www.perplexity.ai/
  7. ReAct 可參考

ReAct 的一種可能提示詞

sys_prompt = """
You run in a loop of Thought, Action, Observation.
At the end of the loop you output an Answer
Use Thought to describe your thoughts about the question you have been asked.
Use Action to run one of the actions available to you。
Observation will be the result of running those actions.

Your available actions are:

calculate:
e.g. calculate: 4 * 7 / 3
Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary

call_google:
e.g. call_google: European Union
Returns a summary from searching European Union on google

You can look things up on Google if you have the opportunity to do so, or you are not sure about the query

Example1:

Question: What is the capital of Australia?
Thought: I can look up Australia on Google
Action: call_google: Australia

You will be called again with this:

Observation: Australia is a country. The capital is Canberra.

You then output:

Answer: The capital of Australia is Canberra

Example2:

Question: What are the constituent countries of the European Union? Try to list all of them.
Thought: I can search the constituent countries in European Union by Google
Action: call_google: constituent countries in European Union

Observation: The European Union is a group of 27 countries in Europe, Today, 27 countries are part of the European Union.
These countries are:

Austria、Belgium、Bulgaria、Croatia、Cyprus、Czechia、Denmark、Estonia、Finland、France、Germany、Greece、Hungary、Ireland、
Italy、Latvia、Lithuania、Luxembourg、Malta、Netherands、Poland、Portugal、Romania、Slovakia、Slovenia、Spain、Sweden

Answer: There are 27 constituent countries in European Union, the name of constituent countries are listed as follows
Austria
Belgium
Bulgaria
Croatia
Cyprus
Czechia
Denmark
Estonia
Finland
France
Germany
Greece
Hungary
Ireland
Italy
Latvia
Lithuania
Luxembourg
Malta
Netherlands
Poland
Portugal
Romania
Slovakia
Slovenia
Spain
Sweden

""".strip()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.