LAB1

Published by onesixx on

 

 

library(tensorflow)

# Check TF version
names(tf)
tf$VERSION

##################################################
# Hello
##################################################
# This op is added as a node to the default graph
hello <- tf$constant("Hello, TensorFlow!") # Create a constant op
sess <- tf$Session()      # start a TF session
print(sess$run(hello))    # run the op and get result

##################################################
# Tensors
#################################################
c(3) # a rank 0 tensor; this is a scalar with shape []
c(1., 2., 3.) # a rank 1 tensor; this is a vector with shape [3]
c(c(1., 2., 3.), c(4., 5., 6.)) # a rank 2 tensor; a matrix with shape [2, 3]
c(c(c(1., 2., 3.)), c(c(7., 8., 9.))) # a rank 3 tensor with shape [2, 1, 3]


node1 <- tf$constant(3.0, tf$float32)
node2 <- tf$constant(4.0)             # also tf.float32 implicitly
node3 <- tf$add(node1, node2)


print(paste("node1:", node1))
print(paste("node2:", node2))
print(paste("node3:", node3))


sess <- tf$Session()
print(paste("sess.run(node1, node2): ", sess$run(c(node1, node2))))
print(paste("sess.run(node3): ", sess$run(node3)))


a <- tf$placeholder(tf$float32)
b <- tf$placeholder(tf$float32)
adder_node <- a + b  # + provides a shortcut for tf.add(a, b)

cat(sess$run(adder_node, feed_dict=dict(a= 3, b=4.5) ) )

cat(sess$run(adder_node, feed_dict=dict(a=c(1,3), b=c(2,4) ) ))

 

Categories: DeepLearning

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x