1563590206

# TensorFlow Variables And Placeholders Tutorial With Example

TensorFlow Variables and Placeholders Tutorial With Example is today’s topic. TensorFlow is an open source machine learning framework developed by Google which can be used to the build neural networks and perform a variety of all machine learning tasks.

TensorFlow works on data flow graphs where nodes are the mathematical operations, and the edges are the data in the for tensors, hence the name Tensor-Flow.

Content Overview

• 1 Tensors
• 2 TensorFlow Variables
• 3 TensorFlow Placeholder
• 4 TensorFlow Variables and Placeholders Tutorial

## Tensors

A tensor is a central unit of data in TensorFlow. It consists of primitive values stored in the shape of a multidimensional array. The number of dimensions a tensor has is called its rank.

A rank 0 tensor is just a scalar. To keep things simple, we can say that a tensor in TensorFlow is instead a fancy name of an array and now we call dimension number as rank. One dimensional array or list is rank one tensor, and two-dimensional array or list is two rank tensor.

## TensorFlow Variables

When we train the model, we need to assign some weights and biases throughout the session.

TensorFlow variables can hold the values of biases and weights throughout the session.

You need to one thing keep in mind thatTensorFlow variables need to be initialized.

In TensorFlow variables are of great use when we are training models. As constants, we have to call a constructor to initialize a variable; the initial value can be passed in as an argument.

Variables can easily be added to the computational graph by calling a constructor.

## TensorFlow Placeholder

TensorFlow placeholders are initially empty and are used to feed in the actual training examples.

If we want to inject the data into a computation graph, we have to use the mechanism named as a placeholder. Placeholders are bound inside some expressions. The syntax of the placeholder is following.

``````placeholder(dtype, shape=None, name=None)

``````

Placeholders allow us to not to provide the data in advance for operations and computation graphs, and the data can be added in runtime from external sources as we train the Machine Learning models.

TensorFlow Placeholder does need to declare as a float32 datatype within an optional shape parameter.

## TensorFlow Variables and Placeholders Tutorial

Okay, we have covered enough theory, let’s see some practical example of TensorFlow Variables and Placeholders in Python Jupyter Notebook.

You can find the guide about how to install TensorFlow on Mac on this article.

Also, you can find the basics of TesorFlow post.

Now, fire up the Jupyter Notebook and import the TensorFlow.

``````import tensorflow as tf

``````

You can run the cell by keyboard shortcut Ctrl + Enter.

In the next cell, we will write the following code.

``````sess = tf.InteractiveSession()

``````

The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. We do not need to write that with** tf.Session() as sess** code whenever we need to perform some operations.

Once we run the above code, we do not need to start the session again for that Jupyter Notebook file.

Now, let’s define a random tensor using the following code.

``````tensorA = tf.random_uniform((4, 4), 1, 2)
tensorA

``````

Here, we have defined the **4*4 **matrix between the value 1 and 2. The values are random between 1 to 2.

When we try to display the tensorA, we will get the following output.

Here, you can see that the datatype of tensorA is float32.

Now, in the next step, we will define a TensorFlow variable called tensor_var_A.

``````tensor_var_A = tf.Variable(initial_value=tensorA)

``````

Okay, now run the **tensor_var_A **variable.

``````sess.run(tensor_var_A)

``````

You will get an error like below.

So, the error is saying that FailedPreconditionError: Attempting to use uninitialized value Variable.

That means, we need to first initialize the TensorFlow variable and then we can run that variable.

So, let’s do that first. Write the following code in the next cell.

``````init = tf.global_variables_initializer()

``````

Run the above cell and then write the following code in the next cell.

``````sess.run(init)

``````

Run the above cell, and all the variables are initialized. Now, we write that failed code again, and now you can see the 4*4 matrix.

``````sess.run(tensor_var_A)

``````

See the output below.

Now, let’s create a TensorFlow Placeholder Example.

Define one placeholder using the following code in the next cell.

``````tfph = tf.placeholder(tf.float32, shape=(None, 5))

``````

The above code creates a TensorFlow placeholder, and its datatype is float32, and here **None **is the placeholder’s initial value of data. As time goes and our machine learning mode starts training, the data is filled in the placeholder. But, at the starting point, it is None.

We can use another example of TensorFlow Placeholder, which is the following code.

``````a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.add(a, b, name='c')
sess.run(c, feed_dict={a: 2.1, b: 1.9})

``````

Here, we have defined two placeholders and then create the third node to add both placeholders and run the operation. Remember, we are using Interactive Session. The output is following.

So, this is how you can create TensorFlow Variables and Placeholders.

Finally, TensorFlow Variables and Placeholders Tutorial With Example is over.

#python #tensorflow #machine-learning

1677668905

## TS-mockito

Mocking library for TypeScript inspired by http://mockito.org/

## 1.x to 2.x migration guide

1.x to 2.x migration guide

## Main features

• Strongly typed
• IDE autocomplete
• Mock creation (`mock`) (also abstract classes) #example
• Spying on real objects (`spy`) #example
• Changing mock behavior (`when`) via:
• `thenReturn` - return value #example
• `thenThrow` - throw an error #example
• `thenCall` - call custom method #example
• `thenResolve` - resolve promise #example
• `thenReject` - rejects promise #example
• Checking if methods were called with given arguments (`verify`)
• `anything`, `notNull`, `anyString`, `anyOfClass` etc. - for more flexible comparision
• `once`, `twice`, `times`, `atLeast` etc. - allows call count verification #example
• `calledBefore`, `calledAfter` - allows call order verification #example
• Resetting mock (`reset`, `resetCalls`) #example, #example
• Capturing arguments passed to method (`capture`) #example
• Recording multiple behaviors #example
• Readable error messages (ex. `'Expected "convertNumberToString(strictEqual(3))" to be called 2 time(s). But has been called 1 time(s).'`)

## Installation

`npm install ts-mockito --save-dev`

## Usage

### Basics

``````// Creating mock
let mockedFoo:Foo = mock(Foo);

// Getting instance from mock
let foo:Foo = instance(mockedFoo);

// Using instance in source code
foo.getBar(3);
foo.getBar(5);

// Explicit, readable verification
verify(mockedFoo.getBar(3)).called();
verify(mockedFoo.getBar(anything())).called();
``````

### Stubbing method calls

``````// Creating mock
let mockedFoo:Foo = mock(Foo);

// stub method before execution
when(mockedFoo.getBar(3)).thenReturn('three');

// Getting instance
let foo:Foo = instance(mockedFoo);

// prints three
console.log(foo.getBar(3));

// prints null, because "getBar(999)" was not stubbed
console.log(foo.getBar(999));
``````

### Stubbing getter value

``````// Creating mock
let mockedFoo:Foo = mock(Foo);

// stub getter before execution
when(mockedFoo.sampleGetter).thenReturn('three');

// Getting instance
let foo:Foo = instance(mockedFoo);

// prints three
console.log(foo.sampleGetter);
``````

### Stubbing property values that have no getters

Syntax is the same as with getter values.

Please note, that stubbing properties that don't have getters only works if Proxy object is available (ES6).

### Call count verification

``````// Creating mock
let mockedFoo:Foo = mock(Foo);

// Getting instance
let foo:Foo = instance(mockedFoo);

// Some calls
foo.getBar(1);
foo.getBar(2);
foo.getBar(2);
foo.getBar(3);

// Call count verification
verify(mockedFoo.getBar(1)).once();               // was called with arg === 1 only once
verify(mockedFoo.getBar(2)).twice();              // was called with arg === 2 exactly two times
verify(mockedFoo.getBar(between(2, 3))).thrice(); // was called with arg between 2-3 exactly three times
verify(mockedFoo.getBar(anyNumber()).times(4);    // was called with any number arg exactly four times
verify(mockedFoo.getBar(2)).atLeast(2);           // was called with arg === 2 min two times
verify(mockedFoo.getBar(anything())).atMost(4);   // was called with any argument max four times
verify(mockedFoo.getBar(4)).never();              // was never called with arg === 4
``````

### Call order verification

``````// Creating mock
let mockedFoo:Foo = mock(Foo);
let mockedBar:Bar = mock(Bar);

// Getting instance
let foo:Foo = instance(mockedFoo);
let bar:Bar = instance(mockedBar);

// Some calls
foo.getBar(1);
bar.getFoo(2);

// Call order verification
verify(mockedFoo.getBar(1)).calledBefore(mockedBar.getFoo(2));    // foo.getBar(1) has been called before bar.getFoo(2)
verify(mockedBar.getFoo(2)).calledAfter(mockedFoo.getBar(1));    // bar.getFoo(2) has been called before foo.getBar(1)
verify(mockedFoo.getBar(1)).calledBefore(mockedBar.getFoo(999999));    // throws error (mockedBar.getFoo(999999) has never been called)
``````

### Throwing errors

``````let mockedFoo:Foo = mock(Foo);

when(mockedFoo.getBar(10)).thenThrow(new Error('fatal error'));

let foo:Foo = instance(mockedFoo);
try {
foo.getBar(10);
} catch (error:Error) {
console.log(error.message); // 'fatal error'
}
``````

### Custom function

You can also stub method with your own implementation

``````let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);

when(mockedFoo.sumTwoNumbers(anyNumber(), anyNumber())).thenCall((arg1:number, arg2:number) => {
return arg1 * arg2;
});

// prints '50' because we've changed sum method implementation to multiply!
console.log(foo.sumTwoNumbers(5, 10));
``````

### Resolving / rejecting promises

You can also stub method to resolve / reject promise

``````let mockedFoo:Foo = mock(Foo);

when(mockedFoo.fetchData("a")).thenResolve({id: "a", value: "Hello world"});
when(mockedFoo.fetchData("b")).thenReject(new Error("b does not exist"));
``````

### Resetting mock calls

You can reset just mock call counter

``````// Creating mock
let mockedFoo:Foo = mock(Foo);

// Getting instance
let foo:Foo = instance(mockedFoo);

// Some calls
foo.getBar(1);
foo.getBar(1);
verify(mockedFoo.getBar(1)).twice();      // getBar with arg "1" has been called twice

// Reset mock
resetCalls(mockedFoo);

// Call count verification
verify(mockedFoo.getBar(1)).never();      // has never been called after reset
``````

You can also reset calls of multiple mocks at once `resetCalls(firstMock, secondMock, thirdMock)`

### Resetting mock

Or reset mock call counter with all stubs

``````// Creating mock
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(1)).thenReturn("one").

// Getting instance
let foo:Foo = instance(mockedFoo);

// Some calls
console.log(foo.getBar(1));               // "one" - as defined in stub
console.log(foo.getBar(1));               // "one" - as defined in stub
verify(mockedFoo.getBar(1)).twice();      // getBar with arg "1" has been called twice

// Reset mock
reset(mockedFoo);

// Call count verification
verify(mockedFoo.getBar(1)).never();      // has never been called after reset
console.log(foo.getBar(1));               // null - previously added stub has been removed
``````

You can also reset multiple mocks at once `reset(firstMock, secondMock, thirdMock)`

### Capturing method arguments

``````let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);

// Call method
foo.sumTwoNumbers(1, 2);

// Check first arg captor values
const [firstArg, secondArg] = capture(mockedFoo.sumTwoNumbers).last();
console.log(firstArg);    // prints 1
console.log(secondArg);    // prints 2
``````

You can also get other calls using `first()`, `second()`, `byCallIndex(3)` and more...

### Recording multiple behaviors

You can set multiple returning values for same matching values

``````const mockedFoo:Foo = mock(Foo);

when(mockedFoo.getBar(anyNumber())).thenReturn('one').thenReturn('two').thenReturn('three');

const foo:Foo = instance(mockedFoo);

console.log(foo.getBar(1));    // one
console.log(foo.getBar(1));    // two
console.log(foo.getBar(1));    // three
console.log(foo.getBar(1));    // three - last defined behavior will be repeated infinitely
``````

Another example with specific values

``````let mockedFoo:Foo = mock(Foo);

when(mockedFoo.getBar(1)).thenReturn('one').thenReturn('another one');
when(mockedFoo.getBar(2)).thenReturn('two');

let foo:Foo = instance(mockedFoo);

console.log(foo.getBar(1));    // one
console.log(foo.getBar(2));    // two
console.log(foo.getBar(1));    // another one
console.log(foo.getBar(1));    // another one - this is last defined behavior for arg '1' so it will be repeated
console.log(foo.getBar(2));    // two
console.log(foo.getBar(2));    // two - this is last defined behavior for arg '2' so it will be repeated
``````

Short notation:

``````const mockedFoo:Foo = mock(Foo);

// You can specify return values as multiple thenReturn args
when(mockedFoo.getBar(anyNumber())).thenReturn('one', 'two', 'three');

const foo:Foo = instance(mockedFoo);

console.log(foo.getBar(1));    // one
console.log(foo.getBar(1));    // two
console.log(foo.getBar(1));    // three
console.log(foo.getBar(1));    // three - last defined behavior will be repeated infinity
``````

Possible errors:

``````const mockedFoo:Foo = mock(Foo);

// When multiple matchers, matches same result:
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(3)).thenReturn('one');

const foo:Foo = instance(mockedFoo);
foo.getBar(3); // MultipleMatchersMatchSameStubError will be thrown, two matchers match same method call
``````

### Mocking interfaces

You can mock interfaces too, just instead of passing type to `mock` function, set `mock` function generic type Mocking interfaces requires `Proxy` implementation

``````let mockedFoo:Foo = mock<FooInterface>(); // instead of mock(FooInterface)
const foo: SampleGeneric<FooInterface> = instance(mockedFoo);
``````

### Mocking types

You can mock abstract classes

``````const mockedFoo: SampleAbstractClass = mock(SampleAbstractClass);
const foo: SampleAbstractClass = instance(mockedFoo);
``````

You can also mock generic classes, but note that generic type is just needed by mock type definition

``````const mockedFoo: SampleGeneric<SampleInterface> = mock(SampleGeneric);
const foo: SampleGeneric<SampleInterface> = instance(mockedFoo);
``````

### Spying on real objects

You can partially mock an existing instance:

``````const foo: Foo = new Foo();
const spiedFoo = spy(foo);

when(spiedFoo.getBar(3)).thenReturn('one');

console.log(foo.getBar(3)); // 'one'
console.log(foo.getBaz()); // call to a real method
``````

You can spy on plain objects too:

``````const foo = { bar: () => 42 };
const spiedFoo = spy(foo);

foo.bar();

console.log(capture(spiedFoo.bar).last()); // [42]
``````

### Thanks

Author: NagRock
Source Code: https://github.com/NagRock/ts-mockito

1596728880

## Tutorial: Getting Started with R and RStudio

In this tutorial we’ll learn how to begin programming with R using RStudio. We’ll install R, and RStudio RStudio, an extremely popular development environment for R. We’ll learn the key RStudio features in order to start programming in R on our own.

If you already know how to use RStudio and want to learn some tips, tricks, and shortcuts, check out this Dataquest blog post.

#data science tutorials #beginner #r tutorial #r tutorials #rstats #tutorial #tutorials

1599097440

## Data Visualization in R with ggplot2: A Beginner Tutorial

A famous general is thought to have said, “A good sketch is better than a long speech.” That advice may have come from the battlefield, but it’s applicable in lots of other areas — including data science. “Sketching” out our data by visualizing it using ggplot2 in R is more impactful than simply describing the trends we find.

This is why we visualize data. We visualize data because it’s easier to learn from something that we can see rather than read. And thankfully for data analysts and data scientists who use R, there’s a tidyverse package called ggplot2 that makes data visualization a snap!

In this blog post, we’ll learn how to take some data and produce a visualization using R. To work through it, it’s best if you already have an understanding of R programming syntax, but you don’t need to be an expert or have any prior experience working with ggplot2

#data science tutorials #beginner #ggplot2 #r #r tutorial #r tutorials #rstats #tutorial #tutorials

1596513720

## 1. Characteristics of Clean Data and Messy Data

What exactly is clean data? Clean data is accurate, complete, and in a format that is ready to analyze. Characteristics of clean data include data that are:

• Free of duplicate rows/values
• Error-free (e.g. free of misspellings)
• Relevant (e.g. free of special characters)
• The appropriate data type for analysis
• Free of outliers (or only contain outliers have been identified/understood), and
• Follows a “tidy data” structure

Common symptoms of messy data include data that contain:

• Special characters (e.g. commas in numeric values)
• Numeric values stored as text/character data types
• Duplicate rows
• Misspellings
• Inaccuracies
• White space
• Missing data
• Zeros instead of null values

## 2. Motivation

In this blog post, we will work with five property-sales datasets that are publicly available on the New York City Department of Finance Rolling Sales Data website. We encourage you to download the datasets and follow along! Each file contains one year of real estate sales data for one of New York City’s five boroughs. We will work with the following Microsoft Excel files:

• rollingsales_bronx.xls
• rollingsales_brooklyn.xls
• rollingsales_manhattan.xls
• rollingsales_queens.xls
• rollingsales_statenisland.xls

As we work through this blog post, imagine that you are helping a friend launch their home-inspection business in New York City. You offer to help them by analyzing the data to better understand the real-estate market. But you realize that before you can analyze the data in R, you will need to diagnose and clean it first. And before you can diagnose the data, you will need to load it into R!

## 3. Load Data into R with readxl

Benefits of using tidyverse tools are often evident in the data-loading process. In many cases, the tidyverse package `readxl` will clean some data for you as Microsoft Excel data is loaded into R. If you are working with CSV data, the tidyverse `readr` package function `read_csv()` is the function to use (we’ll cover that later).

Let’s look at an example. Here’s how the Excel file for the Brooklyn borough looks:

The Brooklyn Excel file

Now let’s load the Brooklyn dataset into R from an Excel file. We’ll use the `readxl`package. We specify the function argument `skip = 4` because the row that we want to use as the header (i.e. column names) is actually row 5. We can ignore the first four rows entirely and load the data into R beginning at row 5. Here’s the code:

``````library(readxl) # Load Excel files
brooklyn <- read_excel("rollingsales_brooklyn.xls", skip = 4)
``````

Note we saved this dataset with the variable name `brooklyn` for future use.

## 4. View the Data with tidyr::glimpse()

The tidyverse offers a user-friendly way to view this data with the `glimpse()` function that is part of the `tibble` package. To use this package, we will need to load it for use in our current session. But rather than loading this package alone, we can load many of the tidyverse packages at one time. If you do not have the tidyverse collection of packages, install it on your machine using the following command in your R or R Studio session:

``````install.packages("tidyverse")
``````

Once the package is installed, load it to memory:

``````library(tidyverse)
``````

Now that `tidyverse` is loaded into memory, take a “glimpse” of the Brooklyn dataset:

``````glimpse(brooklyn)
## Observations: 20,185
## Variables: 21
## \$ BOROUGH <chr> "3", "3", "3", "3", "3", "3", "…
## \$ NEIGHBORHOOD <chr> "BATH BEACH", "BATH BEACH", "BA…
## \$ `BUILDING CLASS CATEGORY` <chr> "01 ONE FAMILY DWELLINGS", "01 …
## \$ `TAX CLASS AT PRESENT` <chr> "1", "1", "1", "1", "1", "1", "…
## \$ BLOCK <dbl> 6359, 6360, 6364, 6367, 6371, 6…
## \$ LOT <dbl> 70, 48, 74, 24, 19, 32, 65, 20,…
## \$ `EASE-MENT` <lgl> NA, NA, NA, NA, NA, NA, NA, NA,…
## \$ `BUILDING CLASS AT PRESENT` <chr> "S1", "A5", "A5", "A9", "A9", "…
## \$ ADDRESS <chr> "8684 15TH AVENUE", "14 BAY 10T…
## \$ `APARTMENT NUMBER` <chr> NA, NA, NA, NA, NA, NA, NA, NA,…
## \$ `ZIP CODE` <dbl> 11228, 11228, 11214, 11214, 112…
## \$ `RESIDENTIAL UNITS` <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1…
## \$ `COMMERCIAL UNITS` <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## \$ `TOTAL UNITS` <dbl> 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1…
## \$ `LAND SQUARE FEET` <dbl> 1933, 2513, 2492, 1571, 2320, 3…
## \$ `GROSS SQUARE FEET` <dbl> 4080, 1428, 972, 1456, 1566, 22…
## \$ `YEAR BUILT` <dbl> 1930, 1930, 1950, 1935, 1930, 1…
## \$ `TAX CLASS AT TIME OF SALE` <chr> "1", "1", "1", "1", "1", "1", "…
## \$ `BUILDING CLASS AT TIME OF SALE` <chr> "S1", "A5", "A5", "A9", "A9", "…
## \$ `SALE PRICE` <dbl> 1300000, 849000, 0, 830000, 0, …
## \$ `SALE DATE` <dttm> 2020-04-28, 2020-03-18, 2019-0…
``````

The `glimpse()` function provides a user-friendly way to view the column names and data types for all columns, or variables, in the data frame. With this function, we are also able to view the first few observations in the data frame. This data frame has 20,185 observations, or property sales records. And there are 21 variables, or columns.

#data science tutorials #beginner #r #r tutorial #r tutorials #rstats #tidyverse #tutorial #tutorials

1596584126

## R Tutorial: Better Blog Post Analysis with googleAnalyticsR

In my previous role as a marketing data analyst for a blogging company, one of my most important tasks was to track how blog posts performed.

On the surface, it’s a fairly straightforward goal. With Google Analytics, you can quickly get just about any metric you need for your blog posts, for any date range.

But when it comes to comparing blog post performance, things get a bit trickier.

For example, let’s say we want to compare the performance of the blog posts we published on the Dataquest blog in June (using the month of June as our date range).

But wait… two blog posts with more than 1,000 pageviews were published earlier in the month, And the two with fewer than 500 pageviews were published at the end of the month. That’s hardly a fair comparison!

My first solution to this problem was to look up each post individually, so that I could make an even comparison of how each post performed in their first day, first week, first month, etc.

However, that required a lot of manual copy-and-paste work, which was extremely tedious if I wanted to compare more than a few posts, date ranges, or metrics at a time.

But then, I learned R, and realized that there was a much better way.

In this post, we’ll walk through how it’s done, so you can do my better blog post analysis for yourself!

## What we’ll need

To complete this tutorial, you’ll need basic knowledge of R syntax and the tidyverse, and access to a Google Analytics account.

Not yet familiar with the basics of R? We can help with that! Our interactive online courses teach you R from scratch, with no prior programming experience required. Sign up and start today!

You’ll also need the `dyplr``lubridate`, and `stringr` packages installed — which, as a reminder, you can do with the `install.packages()` command.

Finally, you will need a CSV of the blog posts you want to analyze. Here’s what’s in my dataset:

`post_url`: the page path of the blog post

`post_date`: the date the post was published (formatted m/d/yy)

`category`: the blog category the post was published in (optional)

`title`: the title of the blog post (optional)

Depending on your content management system, there may be a way for you to automate gathering this data — but that’s out of the scope of this tutorial!

For this tutorial, we’ll use a manually-gathered dataset of the past ten Dataquest blog posts.

#data science tutorials #promote #r #r tutorial #r tutorials #rstats #tutorial #tutorials