如何在 MacOS 上使用 js-pytorch 和 clojurescript?

问题描述 投票:0回答:1

使用 js-pytorch 和 clojurescript 失败。请评论如何使用clojurescript修复js-pytorch的安装问题

在calva-repl中执行:

(ns server.ros2
  (:require ["js-pytorch" :as torch])) => nil

(def device "gpu") => #'server.ros2/device

在calva-repl中执行:

(torch/randn #js [8 4 5]) => :repl/exception!

; Execution error (TypeError) at (<cljs repl>:1).
; module$node_modules$js_pytorch$dist$index_cjs.randn is 
not a function. (In module$node_modules$js_pytorch$dist
$index_cjs.randn([(8),(4),(5)])',  'module$node_modules
$js_pytorch$dist$index_cjs.randn' is undefined)
 

deps.edn:

{:paths ["src"]
 :aliases {:cljs {:extra-deps {thheller/shadow-cljs {:mvn/version "2.28.16"}}}
           :outdated {;; Note that it is `:deps`, not `:extra-deps`
                      :deps {com.github.liquidz/antq {:mvn/version "RELEASE"}}
                      :main-opts ["-m" "antq.core"]}}
 :deps {reagent/reagent {:mvn/version "1.2.0"}}}

Shadown-cljs.edn:

 {:deps {:aliases [:cljs]}
 :builds {:app {:target :browser
                :output-dir "public/js"
                :asset-path "/js"
                :modules {:main {:init-fn client.core/init}}}
          :server {:target :node-script
                   :output-to "out/server.js"
                   :main server.core/start}}
 :dev-http {8080 "public"}}

package.json

{
  "name": "cljs-express-htmx",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "create-react-class": "^15.7.0",
    "express": "^4.19.2",
    "js-pytorch": "^0.6.0",
    "rclnodejs": "^0.27.4",
    "react": "^18.3.1",
    "react-dom": "^18.3.1"
  },
  "devDependencies": {
    "shadow-cljs": "^2.28.16"
  }
}

用bun安装了js-pytorch:

bun init 
bun add js-pytorch

将 js-pytorch 与 clojurescript 结合使用

   (ns server.ros2
      (:require ["js-pytorch" :as torch]))
    
    (defn example []
        ;; Pass device as an argument to a Tensor or nn.Module
      (let [device "gpu"]
    
          ;; Instantiate Tensors
        (let [x (torch/randn #js [8 4 5])
              w (torch/randn #js [8 5 4] true device)
              b (torch/tensor #js [0.2 0.5 0.1 0.0] true)]
    
            ;; Make calculations
          (let [out (-> (torch/matmul x w)
                        (torch/add b))]
    
              ;; Compute gradients on the whole graph
            (.backward out)
    
              ;; Get gradients from specific Tensors
            (js/console.log (.-grad w))
            (js/console.log (.-grad b))))))
pytorch clojurescript clojurescript-javascript-interop
1个回答
0
投票

这是

js-pytorch
自述文件中使用的示例。

const { torch } = require("js-pytorch");

这不会转化为您拥有的

ns
:require
。相反,它翻译为

(ns server.ros2
  (:require ["js-pytorch" :refer (torch)]))

然后使用

(.randn torch ...)
等等。

或者,或者因为使用

/
语法会更好一点,就像你的代码一样,你可以这样做

(ns server.ros2
  (:require ["js-pytorch$torch" :as torch]))

这基本上只是将

torch
设置为名称空间别名,允许使用
torch/randn
等等。

请参阅shadow-cljs 用户指南中的翻译示例。

© www.soinside.com 2019 - 2024. All rights reserved.