tensorflow / swift-apis

Swift for TensorFlow Deep Learning Library
Apache License 2.0
794 stars 133 forks source link

Use of unresolved identifier 'SGD' #769

Closed Kshitij09 closed 4 years ago

Kshitij09 commented 4 years ago

I'm using latest release of swift for tensorflow (v0.8) on Ubuntu 18.04. I've followed all the setup instructions and trying to run following code on jupyter lab using swift-jupyter

import Foundation
import Tensorflow
import Datasets
import ImageClassificationModels

let batchers = ImagenetteBatchers(
    inputSize: .resized320, 
    outputSize: 224,
    batchSize: 64)

var model = MobileNetV1(classCount: 10)

let optimizer = SGD()

(Imagine each block is a code cell of notebook) it works perfect till model creation but immediate next cell of instantiating SGD throws unresolved reference error

let optimizer = SGD()

error: <Cell 14>:1:17: error: use of unresolved identifier 'SGD'
let optimizer = SGD()
                ^~~

Platform details

Swift version 5.2-dev (LLVM b3057cffb6, Swift da7410955d)
Target: x86_64-unknown-linux-gnu
s4tf toolchain: v0.8 (CPU only)

Also, let me know if there's any way I can check the current version of s4tf toolchain through CLI or code snippet (like we do with swift --version)

Kshitij09 commented 4 years ago

The above issue doesn't occur on Google Colab and colab was even able to import MobileNetv2 which is not the case on my local machine.

dan-zheng commented 4 years ago
error: <Cell 14>:1:17: error: use of unresolved identifier 'SGD'
let optimizer = SGD()
                ^~~

Does import TensorFlow instead of import Tensorflow fix the issue?

Invalid module imports are weirdly not diagnosed, which is a recent regression tracked by TF-1210.


Also, let me know if there's any way I can check the current version of s4tf toolchain through CLI or code snippet (like we do with swift --version)

You can run a short script:

import PythonKit

func printSwiftVersion() {
    let subprocess = Python.import("subprocess")
    let version = subprocess.check_output("/swift/toolchain/usr/bin/swift --version", shell: true)
    print(version)
}
printSwiftVersion()
b'Swift version 5.2-dev (LLVM b3057cffb6, Swift da7410955d)\nTarget: x86_64-unknown-linux-gnu\n'

This mailing list thread has more context on running scripts in Swift Jupyter/Colab.

vballoli commented 4 years ago

In addition to @dan-zheng 's suggestion, it might raise an error again since SGD's initialization is missing a necessary argument, i.e

var model = MobileNetV1(classCount: 10) // In your case
let opt = SGD(for: model) // additional arguments SGD(for:learningRate:momentum: decay:nesterov:)

Hope this helps.

Kshitij09 commented 4 years ago

Does import TensorFlow instead of import Tensorflow fix the issue?

This does solve the issue. It should have complained at the first place :sweat_smile:

You can run a short script:

This would print out the current swift version, I was asking for s4tf toolchain version

it might raise an error again since SGD's initialization is missing a necessary argument

Those arguments do have default values.

@dan-zheng should I close this issue or change its title accordingly?

Kshitij09 commented 4 years ago

colab was even able to import MobileNetv2 which is not the case on my local machine.

Also, what could be the reason for this one? does colab update the toolchain nightlies regularly?

dan-zheng commented 4 years ago

colab was even able to import MobileNetv2 which is not the case on my local machine.

Also, what could be the reason for this one?

MobileNetv2 is defined in tensorflow/swift-models, so you need to add tensorflow/swift-models as a SwiftPM dependency in Jupyter/Colab before you can import it.

Try these two cells:

%install '.package(url: "https://github.com/tensorflow/swift-models.git", .branch("master"))' Datasets ImageClassificationModels
import Foundation
import TensorFlow
import Datasets
import ImageClassificationModels

let batchers = ImagenetteBatchers(
    inputSize: .resized320, 
    outputSize: 224,
    batchSize: 64)

var model = MobileNetV1(classCount: 10)

let optimizer = SGD(for: model)

does colab update the toolchain nightlies regularly?

We actually update Colab per Swift for TensorFlow release, not nightly.

dan-zheng commented 4 years ago

@dan-zheng should I close this issue or change its title accordingly?

Feel free to close issues when your questions are all answered!

Kshitij09 commented 4 years ago

so you need to add tensorflow/swift-models as a SwiftPM dependency in Jupyter/Colab before you can import it.

I've actually done this one,

%install-location $cwd/swift-packages
%install '.package(url: "https://github.com/tensorflow/swift-models.git", .branch("master"))' Datasets ImageClassificationModels

but on local machine it shows only the MobileNetV1

Kshitij09 commented 4 years ago

I learned the reason behind it. On your local machine, once you run above installation commands, a directory named swift-packages is being created which acts as cache and confines reinstalling the latest version. You can simply delete swift-packages directory and enjoy latest version but I hope there must be some kind flag (like --no-cache-dir of pip) that I'm missing.

cc: @dan-zheng