Many investors pay close attention to what Warren Buffett is buying, trying to formulate a strategy to build their own winning portfolios. However, knowing when to walk away from an investment is just as important as knowing when to buy. Here are three situations when Warren Buffett unloaded some of his stocks, and what you can learn from them.

Your original reasons for buying no longer apply
Up until a little over a year ago, Berkshire Hathaway (NYSE:BRK.A) (NYSE:BRK.B) was one of ExxonMobil's (NYSE:XOM) largest shareholders.

Buffett has said (and still does) that ExxonMobil is an excellent company. However, at the time Berkshire invested $3.4 billion in ExxonMobil oil prices were high, and Buffett and his team had an optimistic view of the company and oil in general.

In fact, Buffett's business partner Charlie Munger said in 2013: "Oil is absolutely certain to become incredibly short in supply and very high-priced." And, while this may eventually be the case, he was obviously off on the time frame.

Less than two years after making his big investment, Berkshire unloaded its entire ExxonMobil stake in the fourth quarter of 2014. In an interview, Buffett said "I did get less enthusiastic about crude oil prices at the time we owned Exxon." He went on to say that he didn't think oil's future was going to be as bright as many people were thinking.

The bottom line is that is you buy a stock because you're optimistic about its business, and then the opposite happens, it's OK to admit you were wrong and get out.

The company starts getting greedy
In the 1980s, Warren Buffett bought a large stake of Freddie Mac stock, saying that shares were "ridiculously cheap". And, he was right. By 1998, Berkshire owned about 9% of Freddie Mac, and the stake was worth about $3.9 billion – about 12 times Berkshire's initial investment.

However, Buffett saw signs of trouble in the late 1990s. He noticed that the company was too focused on its quarterly results, and started taking on much more risk than it should. Former Freddie Mac CEO Leland Brendsel was focused on delivering double-digit earnings growth, and started making investments that had nothing to do with the company's business model. As a result, Buffett said that he "was concerned about what they might be doing...that I didn't know about."

Buffett follows the saying "there's never just one cockroach in the kitchen," and proceeded to sell substantially all of Berkshire's Freddie Mac shares by 2000.

We all know what happened in over the next decade. Freddie Mac (and others') greed got the best of them, and pushed the mortgage system and the U.S. economy to the brink of collapse. Freddie Mac is under government conservatorship, and Buffett's 9% stake would be worth less than $90 million today.

When you see signs of greed, or that management is doing things that don't make any sense in the company's business model, it may be time to get out.

You'd rather put your money to work elsewhere
Finally, I'd like to mention two stocks that Buffett sold recently: Goldman Sachs (NYSE: GS) and Wal-Mart (NYSE: WMT). Now, Buffett didn't sell all of his shares. He got rid of 13% of Berkshire's Goldman shares and 7% of its Wal-Mart shares.

Now, I don't want to go so far as to say Buffett had no company-specific reasons whatsoever to reduce his exposure to these stocks, because I don't know that for sure, but he did reveal his primary motivation for the sale: Berkshire had decided to acquire Precision Castparts for $32 billion, and Buffett wanted to raise cash to help finance the acquisition.

Buffett has said in the past that he prefers owning entire companies to owning stocks, so it's fair to say that he felt moving some money from his stock portfolio into a fully owned business was a smart move.

Now, I realize that most readers aren't going to acquire a multibillion-dollar business, but the same logic applies to your portfolio: If you feel that you can use your money better, don't be afraid to reduce your exposure to some of your stocks.

Know when to walk away
Warren Buffett has said in regards to Berkshire's stock portfolio that "Our favorite holding period is forever." And, while it's a solid investment strategy to buy stocks with the intention of holding them forever, it's equally important to understand that there are some valid reasons to sell.

This article represents the opinion of the writer, who may disagree with the “official” recommendation position of a Motley Fool premium advisory service. We’re motley! Questioning an investing thesis -- even one of our own -- helps us all think critically about investing and make decisions that help us become smarter, happier, and richer.