Picture of 4d shapes8/11/2023 The optional second parameter would be the data type for the generated tensor. So, the code tf.zeros() would create a 1024 x 768 black image. Both tf.ones and tf.zeros take a shape as a parameter, and then they construct that shape with every value equal to 1 or 0, respectively. In fact, if you were to continue working on our recommendation system from Chapter 3, you would need to utilize these exact features.Īs it is now, you can use the methods tf.ones, tf.zeros, and tf.fill to create large tensors by hand. It’s a common need to create large tensors with already populated values. Of course, you could create arrays using and then use that to fill arrays to create sizable 3D tensor constructors, but it’s worth noting that TensorFlow.js comes with that functionality built in. I know there’s a line of people beating down your door to hand-draw images, pixel by pixel, so you might be surprised to learn some people find writing little 1s and 0s tedious. tensor (,], ,], ,] ])Īnd yes, if you simply remove those inner brackets and move this to a simple 2D tensor, that will work, too! Now we can build the same image but with the tensor, with code like so: const checkySmalls = tf. Our black-and-white checkered pattern example from earlier could be significantly condensed using that last bit of knowledge. Īs a corollary to the two aforementioned systems, if the final channel has only one value instead of three or four, the resulting image would be grayscale. A 1024 x 768 image with transparency would be stored in a tensor with the shape. Much like in web color, #FFFFFF00 would be white at zero opacity, and a tensor pixel with a red, green, blue, alpha (RGBA) value of would be similarly transparent. You’re not limited to RGB, as you might have expected adding a fourth value to the RGB dimension of a tensor will add an alpha channel. This also means is pure white in a float32 tensor and interpreted as next to black in an int32 tensor.ĭepending on the tensor data type, you get two color extremes from a pixel encoded as, as shown in Figure 4-1.įigure 4-2. So, an integer represents pure white, but in float form the equivalent would be. When our tensor is float32, the values are assumed to be in the range 0-1. When our tensor is the data type int32, this is the interpretation method that is used. This 0-255 array of values looks like for integers and like #FFFFFF for most websites seeking a hexadecimal version of the same three values. Your art teacher probably used yellow over green to help lighten up paints that darken as you add more in a “subtractive” color system, like paint on paper.Ī pixel is commonly colored by the ordered amounts of red, green, and blue within the confines of a singular byte. The human eye uses RGB, which is based on an “additive” color system-a system of emitting light, like computer screens do. The RGB system is another instance of art imitating life. When you finish this chapter, you’ll be confident in managing real-world image data, and a lot of this knowledge will apply to managing tensors in general. Identify what makes a tensor an image tensorĬonvert existing images to tensors and back We’ll need to identify some common practices and tricks of the trade, so you don’t have to reinvent the wheel every time. During this exercise with a simple 3 x 3 grid, you identified one method for converting a game’s state, but another person might have come up with a completely different strategy. As you recall, in the previous chapter, you converted a tic-tac-toe game to tensors. We’ll also get to utilize some existing best practices. This is, of course, true in the world of images! This is an exciting chapter because you’ll start working with real data, and we’ll be able to see the effects of your tensor operations immediately. You’re going to need to learn how to deal with large tensors, which are more common. As you might guess, printing tensors can take you only so far and in so many dimensions. In the previous chapter, you created and destroyed simple tensors.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |