FluxML / Torch.jl

Sensible extensions for exposing torch in Julia.
Other
213 stars 15 forks source link

error: `getindex not defined for Tensor{Float32,2}` #43

Open lbotecur opened 3 years ago

lbotecur commented 3 years ago

Hello,

I am implementing a very simple example with Torch.jl, and I get the following error: getindex not defined for Tensor{Float32,2}. The code works for cpu and gpu, and it is as follows:

using Flux
using Flux: params, update!, Losses
using CUDA
using Dates: now
using Statistics: mean
using Random
using Torch
using Torch: torch

if CUDA.has_cuda_gpu()
    #device = gpu;
    device = torch;
    println("Training on GPU")
    println(" - CUDA version: $(CUDA.version())")
    println(" - Device: $(CUDA.name(CUDA.CuDevice(0)))")
else
    device = cpu;
    println("Training on CPU")
end

model = Chain(
    Dense(10, 128, relu),
    Dense(128, 128, relu),
    Dense(128, 1)
) |> device

p = params(model)

opt = ADAM(3e-4)

x = rand(Float32, 10, 2000) |> device
y = rand(Float32, 1, 2000) |> device

function loss(x, y)
    ŷ = model(x)
    return Losses.mse(ŷ, y; agg=mean)
end

for j = 1:10
    st = now()
    for i = 1:10
        g = gradient(() -> loss(x, y), p)
        update!(opt, p, g)
    end
    println(now() - st)
end

My system information is this:

Julia Version 1.5.1
Commit 697e782ab8 (2020-08-25 20:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU @ 2.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, broadwell)
Environment:
  JULIA_NUM_THREADS = 4

GPU info:

CUDA version: 10.1.0
Device: Tesla T4

Packages info:

CUDA v2.3.0
Flux v0.11.2
IJulia v1.23.1
Torch v0.1.2

Thanks.

emailweixu commented 3 years ago

Got same problem with Julia 1.6.

julia> a=Torch.Tensor(3,4, dev=-1)
3×4 Torch.Tensor{Float32, 2}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
julia> a[1,1]
ERROR: getindex not defined for Torch.Tensor{Float32, 2}