Attempting to print a string with an array

please forgive this very newb question. I am attempting to run through the book “Neural Networks from Scratch with Python” … but with JuliaLang instead

I want to do the following but it’s perhaps my bias towards python getting me to print in a maybe wrong way… how do I concatenate a string with an array to print its contents nicely?

Thanks

## Ch02 - Coding our first Neurons

print("Starting Exercise")

inputs  = [1, 2, 3]
weights = [0.2, 0.3, -0.5]

bias = 2

print("inputs: " + string.(inputs))
print("weights: " + string.(weights))
print("bias: " + string.(bias))
% julia ch02_coding-first-neurons/ex01.jl
Starting ExerciseERROR: LoadError: MethodError: no method matching +(::String, ::Vector{String})
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
  +(::Array, ::Array...) at arraymath.jl:43
  +(::SparseArrays.AbstractSparseMatrixCSC, ::Array) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/sparsematrix.jl:1744
  ...
Stacktrace:
 [1] top-level scope

Replace + with * and everything will work.

2 Likes

seems to yield a different error


print("inputs: "*string.(inputs))  
% julia ch02_coding-first-neurons/ex01.jl
Starting Exercise
ERROR: LoadError: MethodError: no method matching *(::String, ::Vector{String})
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
  *(::Union{AbstractChar, AbstractString}, ::Union{AbstractChar, AbstractString}...) at strings/basic.jl:260
  *(::Union{SparseArrays.AbstractSparseMatrixCSC{TA, Ti}, SubArray{TA, 2, var"#s832", Tuple{Base.Slice{Base.OneTo{Int64}}, I}, L} where {I<:AbstractUnitRange, var"#s832"<:SparseArrays.AbstractSparseMatrixCSC{TA, Ti}, L}} where Ti, ::Union{StridedVector{T} where T, BitVector}) where TA at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/linalg.jl:50
  ...
Stacktrace:
 [1] top-level scope
   @ ~/nnfs-julialang/ch02_coding-first-neurons/ex01.jl:10
in expression starting at /nnfs-julialang/ch02_coding-first-neurons/ex01.jl:10

You may be interested in this page: https://towardsdatascience.com/how-to-build-an-artificial-neural-network-from-scratch-in-julia-c839219b3ef8

1 Like

The easiest way to do this is to skip calling string at all:

julia> inputs = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> println("input: ", inputs)
input: [1, 2, 3]

The println function accepts multiple arguments, and it already knows how to print arrays.

Even easier, you can use the @show macro:

julia> @show inputs
inputs = [1, 2, 3]
3 Likes

I figured it out with the following change

## Ch02 - Coding our first Neurons

println("Starting Exercise")

inputs  = [1, 2, 3]
weights = [0.2, 0.3, -0.5]

bias = 2

print("inputs: ",string.(inputs))

Ouput:

% julia ch02_coding-first-neurons/ex01.jl
Starting Exercise
inputs: ["1", "2", "3"]%   

I guess my final question is… am I experiencing different answers based on different versions of Julia ?

I saw some references to

@printf

https://docs.julialang.org/en/v1/stdlib/Printf/

this suggested * didn’t work either, as mentioned earlier in my last post…

I don’t think so–you can use * to concatenate two strings in any Julia version. What you can’t do is concatenate a string to a vector of strings, which is what string.(inputs) produces:

julia> "hello" * "world"
"helloworld"

julia> "hello" * ["world", "world"]
ERROR: MethodError: no method matching *(::String, ::Vector{String})
2 Likes

You can do that too. Printf is a package in the standard library, so you need to load it with using or import first:

julia> using Printf

julia> @printf "inputs: %s" inputs
inputs: [1, 2, 3]
3 Likes